kk003

kk003_telegram # V1.4 for nvov 19+

Oct 23rd, 2017
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 89.47 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Telegram Info/Alerts Script
  4. # for nvOC by fullzero
  5. # mod by kk003 ;-) + terex
  6. # Script name: kk003_telegram
  7. # Version: 1.4
  8.  
  9. # You must create a bot and get your new user/chat Id + Api key.
  10. # Tutorials:
  11. # Tutorial how to create a telegram bot: http://bernaerts.dyndns.org/linux/75-debian/351-debian-send-telegram-notification
  12. # Video tutorial how to create a telegram bot by kk003: https://vimeo.com/252156920
  13. # Enlace hilo foro para OP+video+instalar en nvoc 19 estable
  14.  
  15. #####
  16. # if you fancy the script you can make a donation. Will be greatly appreciated
  17. # BTC:   0x196pMZnByg5UaPvJPK67ErAHgDPeSyu8QV
  18. # ETH:   0x628dD8d1264543292C68F4C587012BaCe805891A
  19. # ETC:   0x60d3349263a313e86DFCFA090FB12169A786D0E9
  20. # EXP:   0xF590cE35961D0B22aFEd57cc35FDE96E8802d0bC
  21. # MUSIC: 0x40b44982642acf21b7bde576a9f555cad94f6b9b
  22. # ZEC:   0xt1UuA5MKU18kJKMEyuQaVpdNtm552kHkDo3
  23. #####
  24.  
  25. #
  26. ## Keep this block of code at the begining of script or it will complain about name of the log file
  27. #
  28.  
  29. NVOC_VERSION=$(cat ~/1bash | grep "nvOC_1bash_ver=" | cut -d"=" -f2 | cut -d" " -f1  | sed 's/"//g')
  30.  
  31. if [[ "$NVOC_VERSION" != "v0019-2.0.002" ]]; then
  32.     KK003_WORKBENCH=~/kk003_telegram_data
  33. else
  34.     KK003_WORKBENCH=~/nvoc_logs/kk003_telegram_data
  35. fi
  36.  
  37. # Log file location
  38. KK003_LOG_FILE=$KK003_WORKBENCH/kk003_telegram.log
  39.  
  40. # I need to have some data over restarts like ips. I'll use a file to do so
  41. DATA_FILE=$KK003_WORKBENCH/data
  42.  
  43. # Create the working dirs so I keep things tidy
  44. if [[ ! -d $KK003_WORKBENCH ]]; then
  45.     mkdir $KK003_WORKBENCH
  46. fi
  47.  
  48. if [[ ! -d $KK003_WORKBENCH/files ]]; then
  49.     mkdir $KK003_WORKBENCH/files
  50. fi
  51.  
  52. # Create the log file
  53. if [[ ! -f "$KK003_LOG_FILE" ]]; then
  54.     touch $KK003_LOG_FILE
  55. fi
  56.  
  57. # Create the file to hold some data I'll need over restarts.
  58. if [[ ! -f $DATA_FILE ]]; then
  59.     touch $DATA_FILE
  60. fi
  61.  
  62. START_TIME=`date "+%Y-%m-%d %H:%M:%S"`
  63.  
  64. #
  65. ## End of block of code at the begining of script
  66. #####
  67.  
  68. ( ##### DON'T REMOVE THIS OPEN PARENTHESIS OR WE'LL MISS THE LOG FILE, NOR CALL FUNCTIONS FROM ABOVE IT OR YOU GET A ERROR
  69.  
  70. ### Some vars related to telegram and sending alerts you may want to customize
  71. #
  72. TELEGRAM_CURRENT_CHARS_LIMT=4096    # Telegram gives a "400 BAD_REQUEST MESSAGE_TOO_LONG" if length >= 4096 UTF8 characters
  73.                     # so I will check the length before send msg
  74.  
  75. USE_NVOC_DEFAULT_TIMES_TELEGRAM=0   # 0, send a telegram when this script thinks is right (warnings) or when its time to do so as var "USER_CUSTOM_TIMES_TO_SEND_TELEGRAM" says
  76.                     # 1, send a telegram's NVOC style, that is at the interval var "TELEGRAM_TIMEOUT_IN_MINUTES" in 1bash says, no matter a problem is detected or not
  77.  
  78. ON_WARNING_NUMBER_TELEGRAMS_TO_SEND=3   # Max number of telegrams to send per warning (NO all warning listen to this var!!)
  79.                     # Warnings auto-clear when resolved or when system/Xorg is restarted but not all warning are counted here
  80.                     # You may get more telegrams than the value used here if there's more
  81.                     # than one warning active at the same time as warnings are independent from each other
  82.  
  83. SYNC_TELEGRAM_SYSTEM=1          # 0, DON'T sync telegram system when value in var TELEGRAM_TIMEOUT_IN_MINUTES in 1bash is changed by user
  84.                     # 1, SYNC telegram system when value in var TELEGRAM_TIMEOUT_IN_MINUTES in 1bash is changed by user
  85.  
  86. USE_USER_CUSTOM_TIMES_TELEGRAM=1    # 0, ignore user custom times to send telegram (in var "USER_CUSTOM_TIMES_TO_SEND_TELEGRAM")
  87.                     # 1, use user custom times to send telegram (allows you to send telegrams at the times of day you want , or almost ;-))
  88.  
  89. # Send msg at certain hours of the day (I have been warned not to use cron so I do this job in the script)
  90. # For this array to work you MUST use the following format:
  91. # 1. 00-23 for hours (notice two digits)
  92. # 2. 00-59 for minutes (notice two digits)
  93. # 3. ":" between hours and minutes
  94. # 4. Values between quotation marks
  95. # 5. Values must be space separated
  96. # Example: USER_CUSTOM_TIMES_TO_SEND_TELEGRAM=("11:00" "16:00" "20:00" )
  97. #
  98. # You'll get a telegram if you use a wrong format!!!
  99. # REMENBER: 00:00 won't work. You won't get a telegram at this time (may be i'll fix it later some time)
  100. # REMENBER: first time of day is 00:01 and last is 23:59
  101. # REMENBER: You'll never get a telegram at 00:00, set it up to 00:01 (yes, again)
  102. # REMENBER: If times you ask here for a telegram are in intervals smaller than value of var "TELEGRAM_TIMEOUT_IN_MINUTES" in /home/m1/1bash
  103. # you'll only get one of them (usually the one closest to time 00:01 of that day)
  104. # When this script is running it checks if time left to send a telegram is less than time left for next check. If so "sleeps" until it's time to send the msg
  105. # So, you better set up var "TELEGRAM_TIMEOUT_IN_MINUTES" in /home/m1/1bash at 10, 15, 20, or 30 minutes max. That way you'll get a rich log file full of info
  106. #
  107. #
  108. USER_CUSTOM_TIMES_TO_SEND_TELEGRAM=("11:00" "16:00" "21:32")
  109.  
  110. # The script supports multi-telegrams. That is it can send the telegram message to several recipients
  111. # In /home/m1/1bash, as allways you still have to add your USER ID to variable "TELEGRAM_CHATID" and your API KEY to varible "TELEGRAM_APIKEY"
  112. # Then add the additional chats that you want to send a telegram to this variable
  113. # Data must be added this way:
  114. # 1. 2 values for chat
  115. # 2. First value is the API KEY
  116. # 3. Second value is the User ID / Channel ID - The first if you send the telegram to a user, the second if you send tehe telegram to a channel
  117. # but the field is the same one for any of them and it has to be only one here
  118. # Example for two additional:
  119. # TELEGRAM_ADDITIONAL_RECIPIENTS=(
  120. #                   "455957604:AAHBEMidb2894YUBZq5dXRSL9lFbgHvE808"
  121. #                   "12345678"
  122. #                   "456545604:4rttyMidb283456yhg5dXRSL9lFbgHv6yhj"
  123. #                   "12309898"
  124. #               )  
  125.  
  126.  
  127. TELEGRAM_ADDITIONAL_RECIPIENTS=(
  128.                                
  129.                                 )
  130.  
  131. # The warnings file holds how many times a warning has occurred for a registered element
  132. #
  133. WARNINGS_FILE=$KK003_WORKBENCH/kk003_warnings   # The warnings file
  134.  
  135. #### Some values you may want to customize for "function my_custom_mods_for_nvidia_smi"
  136. #
  137. CENTIGRADO="ºC"    # Symbol you want after the gpu temp value
  138.  
  139. # Aliases:
  140. MODELS=("GeForce GTX 1060 3GB" "GF1060_3G" "GeForce GTX 1060 6GB" "GF1060_6G" "GeForce GTX 970" "GF970_4G" "GeForce GTX 1070" "GTX 1070" "GeForce GTX 1080" "GTX1080" "P106-100" "P106-6G" ) # Cos telegram cut off the lines I want to make
  141.                                                         # them as short as possible.
  142.                                                         # Just add the gpu name such as is showen by nvidia-smi
  143.                                                         # follow by the alias you want
  144.                                                         # Fields must be space separated and in quotation marks
  145.                                                         # I recommend not to use spaces in aliases
  146.  
  147. USE_GPU_ALIAS="YES"                                             # "YES or "NO" If yes replaces the gpu's name with the alias. See variable "MODELS"
  148.  
  149. # Query arguments for "function my_custom_mods_for_nvidia_smi"
  150. # You can get a complete list of the query arguments by issuing: nvidia-smi --help-query-gpu
  151. # Add/remove what you want here. Args must be coma separated
  152. NVIDIA_SMI_QUERY_ARGUMENTS="index,gpu_name,gpu_bus_id,pstate,utilization.gpu,power.draw,temperature.gpu,fan.speed,clocks.mem,clocks.gr,memory.used"
  153.  
  154. USE_function_my_custom_mods_for_nvidia_smi="YES"    # YES or NO, say "YES" here to use my custom data arrange provided for this function
  155.                             #            say "NO" to get the standar data output from nvidia-smi
  156.  
  157. MODE_NVIDIA_SMI_REPORT=2                # 0 or 1 or 2  0 Don't use minimal output data but query arguments in var "NVIDIA_SMI_QUERY_ARGUMENTS"
  158.                                      # 1 Only minimal output is used and shown as a BLOCK. Overrides query arguments in var  
  159.                                      #   "NVIDIA_SMI_QUERY_ARGUMENTS" and "USE_function_my_custom_mods_for_nvidia_smi" too (see var above).
  160.                                      # 2 Only minimal output is used and shown as MODULES PER ARGUMENT. Overrides query arguments in var
  161.                                                                      #   "NVIDIA_SMI_QUERY_ARGUMENTS" and "USE_function_my_custom_mods_for_nvidia_smi" too (see var above).
  162.  
  163. # Legend for headers on MODE_NVIDIA_SMI_REPORT=1 (function nvidia_smi_minimal_report)
  164. # GID = gpu index
  165. # GPN = gpu name
  166. # GUT = utilization gpu
  167. # GPL = power limit
  168. # GPD = power draw
  169. # GTP = temperature gpu
  170. # GFS = fan speed
  171.  
  172.  
  173. #### Some values you may want to customize for "function system_info" (I think most names are self explanatory)
  174. #
  175. # Some warning you can choose to get via telegram or not
  176. SEND_WARNING_SYSTEM_RESTARTED="YES" # YES or NO
  177. SEND_WARNING_XORG_RESTARTED="YES"   # YES or NO
  178. SEND_WARNING_LOAD_AVERAGE="YES"     # YES or NO
  179. SEND_WARNING_CHECK_DISPLAY="YES"    # YES or NO
  180. #
  181. # some information you can choose to receive or not on your telegram msg
  182. SEND_REBOOT_REQUIRED="YES"      # YES or NO
  183. SEND_WORKERNAME="YES"           # YES or NO
  184. SEND_MINING_COIN="YES"          # YES or NO
  185. SEND_MINING_ADDRESS="YES"       # YES or NO
  186. MASK_MINING_ADDRESS="YES"       # YES or NO
  187. SEND_MINER_EXEC_NAME="YES"      # YES or NO
  188. SEND_MINER_EXEC_PATH="YES"      # YES or NO
  189. SEND_MINER_UP_TIME="YES"        # YES or NO
  190. SEND_PRIVATE_IP="YES"           # YES or NO
  191. SEND_PUBLIC_IP="YES"            # YES or NO
  192. MASK_PUBLIC_IP="NO"             # YES or NO
  193. SEND_SSHD_PORT="YES"            # YES or NO
  194. SEND_UP_SINCE="YES"         # YES or NO
  195. SEND_UP_TIME="YES"          # YES or NO
  196. SEND_RAM="YES"              # YES or NO
  197. SEND_CPU_MODEL="YES"            # YES or NO
  198. SEND_LOGGED_USERS="YES"         # YES or NO
  199.  
  200. CUSTOMIZE_SYSTEM_NAME=""        # "PRIVATE_IP" or "PUBLIC_IP" or "AnY CusTom nAme you want here", if empty it uses the system's hostname
  201.                     # This is the main header you'll get in your telegram message
  202.                     # PRIVATE_IP will use your private ip/s (remenber you may have more than one)
  203.                     # PUBLIC_IP will use your public ip
  204.  
  205. # This is a very important var you MUST customize!!!!
  206. NUMBER_GPUS_FOR_SURE=6          # Number of gpus you know for sure your system must have
  207.                     # Change this value to the number of gpus your system have or you will get a false warning
  208.  
  209. #### Some values you may want to customize for function claymore_statics"
  210. #
  211. USE_CLAYMORE_STATICS="YES"              # YES or NO, if claymore is running use the provided data?
  212. KEY_CLAYMORE="ethdcrminer64"                # String I search for to check if CLAYMORE is running
  213.                             # Different vesions may have different name.
  214.                             # You must check if this entry is right for your
  215.                             # system to get the script runnin correctly (OK for versions 8 to 10 at 2017/09/23 )
  216.  
  217. USE_GENOIL_STATICS="YES"                # YES or NO, if Genoil is running use the provided data?
  218. KEY_GENOIL=~/eth/Genoil-U/ethminer          # String I search for to check if GENOIL is running
  219.                             # Different vesions may have different name.
  220.  
  221. USE_ETHMINER_STATICS="YES"              # YES or NO, if Ethminer is running use the provided data?
  222. KEY_ETHMINER_1=~/eth/ethminer_11/ethminer       # Strings I search for to check if ETHMINER is running
  223. KEY_ETHMINER_2=~/eth/ethminer_12dev2/ethminer       # Different vesions may have different name.
  224. KEY_ETHMINER_3=~/eth/ethminer/latest/ethminer       # Different vesions may have different name.
  225. # Error exit codes (or RETURN VALUE as nvidia call them) for nvidia-smi
  226. # I'll treat them as an array so in couples; firt value is the code number and second value is the text that explains it
  227. NVIDIA_SMI_RETURN_VALUE=(
  228.                 "0" "Success"
  229.                 "2" "A supplied argument or flag is invalid"
  230.                 "3" "The requested operation is not available on target device"
  231.                 "4" "The current user does not have permission to access this device or perform this operation"
  232.                 "6" "A query to find an object was unsuccessful"
  233.                 "8" "A device is external power cables are not properly attached"
  234.                 "9" "NVIDIA driver is not loaded"
  235.                 "10" "NVIDIA Kernel detected an interrupt issue with a GPU"
  236.                 "12" "NVML Shared Library couldn't be found or loaded"
  237.                 "13" "Local version of NVML doesn't implement this function"
  238.                 "14" "infoROM is corrupted"
  239.                 "15" "The GPU has fallen off the bus or has otherwise become inaccessible"
  240.                 "255" "Other error or internal driver error occurred"
  241.          )
  242.  
  243. LF=$'\n' # New line
  244.  
  245. ##
  246. #
  247. # Below here don't change value of vars/code unless you know what you are doing
  248. #
  249. ##
  250.  
  251. # Functions
  252. #
  253.  
  254. function nvidia_smi_minimal_report ()
  255. {
  256.  
  257. # Empty the files
  258. > $KK003_WORKBENCH/files/nvidia_smi_values.csv
  259. > $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  260.  
  261. if [[ $MODE_NVIDIA_SMI_REPORT -eq 1 ]]; then
  262.     # Query the values I want
  263.     nvidia-smi --query-gpu=index,gpu_name,utilization.gpu,power.limit,power.draw,temperature.gpu,fan.speed --format=csv,noheader > $KK003_WORKBENCH/files/nvidia_smi_values.csv
  264.  
  265.     # Mount the header for mode 1
  266.     HEADER_MINIMAL_NVIDIA_SMI="GID GPN GUT GPL GPD GTP GFS"
  267.  
  268.     # Lines in file should be same as gpus
  269.     LINES_TO_MODIFY=$GPU_COUNT
  270.  
  271.     # Arrange the output for the "temperature.gpu" nvidia-smi argument
  272.     # I want to add "ºC"
  273.     #
  274.     # Column number for temperature.gpu in file
  275.     COLUMN_NUMBER_TEMPERATURE_GPU=6
  276.  
  277.     for ((L=1; L <= $LINES_TO_MODIFY ; L=L+1))
  278.     do
  279.     OLD_VALUE=`sed -n -e "${L}p" $KK003_WORKBENCH/files/nvidia_smi_values.csv | cut -d, -f$COLUMN_NUMBER_TEMPERATURE_GPU`      # Extraigo el dato original
  280.  
  281.     NEW_VALUE=$OLD_VALUE$CENTIGRADO
  282.         sed -i "$L"'s/^\(\([^,]*,\)\{'"$(($COLUMN_NUMBER_TEMPERATURE_GPU -1))"'\}\)[^,]*/\1'"$NEW_VALUE"'/' $KK003_WORKBENCH/files/nvidia_smi_values.csv
  283.     done
  284.  
  285.     # Copy the telegram file to the log file
  286.     cp $KK003_WORKBENCH/files/nvidia_smi_values.csv $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  287.  
  288.     # Call function to change gpu's name for alias (modify files for telegram and log file)
  289.     gpu_names_to_alias
  290.  
  291.     # Try to align columns for log file (stupid for telegram but not for the log file)
  292.     cat $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv | column -s, -t > $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv.temp
  293.  
  294.     # Set log file in place
  295.     cp $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv.temp $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  296.  
  297.     # Insert header in log file
  298.     sed -i "1s/^/$HEADER_MINIMAL_NVIDIA_SMI\n/" $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  299.  
  300.     # Change "," for " " in telegram file
  301.     sed -i 's/,/ /g' $KK003_WORKBENCH/files/nvidia_smi_values.csv
  302.  
  303.     # Insert header in telegram file
  304.     sed -i "1s/^/$HEADER_MINIMAL_NVIDIA_SMI\n/" $KK003_WORKBENCH/files/nvidia_smi_values.csv
  305. fi
  306.  
  307. if [[ $MODE_NVIDIA_SMI_REPORT -eq 2 ]]; then
  308.    
  309. # nvidia-smi --query-gpu=index,gpu_name,utilization.gpu,power.limit,power.draw,temperature.gpu,fan.speed --format=csv,noheader | sed 's/ //g' | sed 's/ /\n/g' > $KK003_WORKBENCH/files/nvidia_smi_values.csv
  310.  
  311.     # utilization.gpu
  312.     echo "Gpu_Utilization:" >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  313.     nvidia-smi --query-gpu=index,gpu_name,utilization.gpu --format=csv,noheader  >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  314.     echo  >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  315.  
  316.     # power limt
  317.     echo "Gpu_power_limit:" >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  318.     nvidia-smi --query-gpu=index,gpu_name,power.limit --format=csv,noheader >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  319.     echo  >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  320.  
  321.     # power draw
  322.     echo "Gpu_power_draw:" >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  323.     nvidia-smi --query-gpu=index,gpu_name,power.draw --format=csv,noheader >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  324.     echo  >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  325.  
  326.     # gpu temperature
  327.     echo "Gpu_temperature_($CENTIGRADO):" >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  328.     nvidia-smi --query-gpu=index,gpu_name,temperature.gpu --format=csv,noheader >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  329.     echo  >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  330.  
  331.     # gpu fan speed
  332.     echo "Gpu_fan_speed:" >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  333.     nvidia-smi --query-gpu=index,gpu_name,fan.speed --format=csv,noheader >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  334.     echo  >> $KK003_WORKBENCH/files/nvidia_smi_values.csv
  335.  
  336.     # Set log file in place
  337.     cp $KK003_WORKBENCH/files/nvidia_smi_values.csv $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  338.  
  339.     # Call function to change gpu's name for alias (modify files for telegram and log file)
  340.     gpu_names_to_alias
  341.  
  342.     # Replace " " for nothing and "," for spaces
  343.     sed -i 's/ //g; s/,/ /g;' $KK003_WORKBENCH/files/nvidia_smi_values.csv
  344.     sed -i 's/ //g; s/,/ /g;' $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  345.  
  346.     # Try to align columns for log file (stupid for telegram but not for the log file)
  347.     cat $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv | column -t  > $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv.temp
  348.  
  349.     # Set log file in place
  350.     cp $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv.temp $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  351.  
  352. fi
  353. }
  354.  
  355. function gpu_names_to_alias ()
  356. {
  357. ##### Changing the gpu's name for the alias (if I have it)
  358. #
  359. if [[ "$USE_GPU_ALIAS" == "YES" ]]; then
  360.  
  361.     # First must check that elements in the array are a even number and is not empty
  362.     MODELS_LENGTH=${#MODELS[@]} # Get number of elements
  363.  
  364.     if [[ $MODELS_LENGTH -ne 0 ]]; then # Is not empty
  365.  
  366.     if [[ $((MODELS_LENGTH%2)) -eq 0 ]]; then                                           # Number of elements is even?. If so I assume names and alias are well matched in array
  367.  
  368.             for ((INDEX_EVEN=0; INDEX_EVEN < $MODELS_LENGTH ; INDEX_EVEN=INDEX_EVEN+2))     # First element of array have index 0
  369.             do                                                                              # Even elements are the gpu names, odd elements are alias
  370.  
  371.             NAME_GPU_IN_ARRAY=${MODELS[$INDEX_EVEN]}                                    # Get the name of the gpu in array
  372.             INDEX_ODD=$(($INDEX_EVEN + 1))                                              # +1 to get the alias index
  373.             ALIAS_GPU_IN_ARRAY=${MODELS[$INDEX_ODD]}                                    # Get the alias of the gpu in array
  374.  
  375.             # Find and replace gpu name if exists
  376.             sed -i "s/${NAME_GPU_IN_ARRAY}/${ALIAS_GPU_IN_ARRAY}/g" $KK003_WORKBENCH/files/nvidia_smi_values.csv
  377.  
  378.             sed -i "s/${NAME_GPU_IN_ARRAY}/${ALIAS_GPU_IN_ARRAY}/g" $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  379.             done
  380.  
  381.     # I get this way out if number of elements in array are odd and I don't replace names for alias
  382.     else
  383.             echo "Number of elements in variable MODELS are odd and I don't replace gpu names for alias."
  384.             echo "Check variable MODELS if you want to replace names for alias"
  385.     fi
  386.  
  387.     # I get this way out if array in variable MODELS is empty
  388.     else
  389.     echo "Variable MODELS is empty and I don't replace gpu names for alias."
  390.     echo "Check variable MODELS if you want to replace names for alias"
  391.     fi
  392.  
  393. fi
  394.  
  395. ##### END of Changing gpu's name by the alias (if I have it)
  396.  
  397. }
  398.  
  399. function my_custom_mods_for_nvidia_smi ()
  400. {
  401.  
  402. # I organize some things my way
  403. # comment out the call to this function and you will get the standar output from nvidia-smi
  404. ###
  405. # Beginning of block
  406. ###
  407.  
  408. # Save header and remove it from the csv file
  409. CSV_HEADER=$(cat $KK003_WORKBENCH/files/nvidia_smi_values.csv | head -n 1 | sed 's/ //g' | sed 's/,/ /g')   # Save the header of csv file
  410. sed -i -e "1d" $KK003_WORKBENCH/files/nvidia_smi_values.csv                     # Remove the header of telegram file (first line)
  411.  
  412. sed -i -e "1d" $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv                    # Remove the header of log file (first line)
  413.  
  414. # Call function to change gpu's name for alias
  415. gpu_names_to_alias
  416.  
  417. # I don't want any spaces but to align thing
  418. sed -i 's/ //g' $KK003_WORKBENCH/files/nvidia_smi_values.csv
  419.  
  420. sed -i 's/ //g' $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  421.  
  422. # Get number of elements on NVIDIA_SMI_QUERY_ARGUMENTS variable
  423. NVIDIA_SMI_QUERY_ARGUMENTS_LENGTH=$(echo $NVIDIA_SMI_QUERY_ARGUMENTS | sed 's/[^,]//g' | wc -c)
  424.  
  425. # Review all elements in variable NVIDIA_SMI_QUERY_ARGUMENTS and do some mods about the output for some fields
  426. for ((INDEX=1; INDEX <= $NVIDIA_SMI_QUERY_ARGUMENTS_LENGTH ; INDEX=INDEX+1)) # Gives me the column number
  427. do
  428.  
  429.     NVIDIA_SMI_ARGUMENT=$(echo $NVIDIA_SMI_QUERY_ARGUMENTS | cut -d, -f $INDEX)    # Gives me the name of nvidia-smi argument on array
  430.  
  431.     #### Arrange the output for the "index" nvidia-smi argument
  432.     #### I want to aling numbers <= 9 if I have more than 9 gpus so I add a space in front
  433.  
  434.     if [[ "$NVIDIA_SMI_ARGUMENT" == "index" && $GPU_COUNT -gt 10 ]]; then
  435.     LINES_TO_MODIFY=10
  436.  
  437.     for ((L=1; L <= $LINES_TO_MODIFY ; L=L+1))
  438.     do
  439.  
  440.         OLD_VALUE=`sed -n -e "${L}p" $KK003_WORKBENCH/files/nvidia_smi_values.csv | cut -d, -f$INDEX`      # Extraigo el dato original
  441.         NEW_VALUE=" "$OLD_VALUE
  442.         sed -i "$L"'s/^\(\([^,]*,\)\{'"$(($INDEX -1))"'\}\)[^,]*/\1'"$NEW_VALUE"'/' $KK003_WORKBENCH/files/nvidia_smi_values.csv
  443.  
  444.         sed -i "$L"'s/^\(\([^,]*,\)\{'"$(($INDEX -1))"'\}\)[^,]*/\1'"$NEW_VALUE"'/' $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  445.     done
  446.     fi
  447.  
  448.     #### END of Arrange the output for the "index" nvidia-smi argument
  449.  
  450.     #### Arrange the output for the "pci.bus_id" nvidia-smi argument
  451.     #### I don't want the "00000000:" at the beginning
  452.  
  453.     if [[ "$NVIDIA_SMI_ARGUMENT" == "gpu_bus_id" ]]; then
  454.         LINES_TO_MODIFY=$GPU_COUNT
  455.  
  456.         for ((L=1; L <= $LINES_TO_MODIFY ; L=L+1))
  457.         do
  458.  
  459.             OLD_VALUE=`sed -n -e "${L}p" $KK003_WORKBENCH/files/nvidia_smi_values.csv | cut -d, -f$INDEX`      # Extraigo el dato original
  460.             NEW_VALUE=$(echo "$OLD_VALUE" | sed "s/00000000://")
  461.             sed -i "$L"'s/^\(\([^,]*,\)\{'"$(($INDEX -1))"'\}\)[^,]*/\1'"$NEW_VALUE"'/' $KK003_WORKBENCH/files/nvidia_smi_values.csv
  462.  
  463.         sed -i "$L"'s/^\(\([^,]*,\)\{'"$(($INDEX -1))"'\}\)[^,]*/\1'"$NEW_VALUE"'/' $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  464.         done
  465.     fi
  466.  
  467.     #### END of Arrange the output for the "index" nvidia-smi argument
  468.  
  469.  
  470.     #### Arrange the output for the "utilization.gpu" nvidia-smi argument
  471.     #### I want to aling values to the right and keep the "%"
  472.  
  473.     if [[ "$NVIDIA_SMI_ARGUMENT" == "utilization.gpu" ]]; then
  474.     LINES_TO_MODIFY=$GPU_COUNT
  475.  
  476.     for ((L=1; L <= $LINES_TO_MODIFY ; L=L+1))
  477.     do
  478.  
  479.         OLD_VALUE=`sed -n -e "${L}p" $KK003_WORKBENCH/files/nvidia_smi_values.csv | cut -d, -f$INDEX`      # Extraigo el dato original
  480.         LEN=${#OLD_VALUE}
  481.  
  482.         case $LEN in
  483.  
  484.         2)  NEW_VALUE="  "$OLD_VALUE
  485.         ;;
  486.         3)  NEW_VALUE=" "$OLD_VALUE
  487.         ;;
  488.         *)  NEW_VALUE=$OLD_VALUE
  489.         ;;
  490.         esac
  491.  
  492.         sed -i "$L"'s/^\(\([^,]*,\)\{'"$(($INDEX -1))"'\}\)[^,]*/\1'"$NEW_VALUE"'/' $KK003_WORKBENCH/files/nvidia_smi_values.csv
  493.  
  494.         sed -i "$L"'s/^\(\([^,]*,\)\{'"$(($INDEX -1))"'\}\)[^,]*/\1'"$NEW_VALUE"'/' $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  495.     done
  496.     fi
  497.  
  498.     #### END of Arrange the output for the "utilization.gpu" nvidia-smi argument
  499.  
  500.  
  501.     #### Arrange the output for the "temperature.gpu" nvidia-smi argument
  502.     #### I want to aling values to the right and add the "ºC"
  503.  
  504.     if [[ "$NVIDIA_SMI_ARGUMENT" == "temperature.gpu" ]]; then
  505.         LINES_TO_MODIFY=$GPU_COUNT
  506.  
  507.         for ((L=1; L <= $LINES_TO_MODIFY ; L=L+1))
  508.         do
  509.             OLD_VALUE=`sed -n -e "${L}p" $KK003_WORKBENCH/files/nvidia_smi_values.csv | cut -d, -f$INDEX`      # Extraigo el dato original
  510.             LEN=${#OLD_VALUE}
  511.  
  512.             case $LEN in
  513.  
  514.         1)  NEW_VALUE="  "$OLD_VALUE$CENTIGRADO
  515.         ;;
  516.             2)  NEW_VALUE=" "$OLD_VALUE$CENTIGRADO
  517.             ;;
  518.             *)  NEW_VALUE=$OLD_VALUE$CENTIGRADO
  519.             ;;
  520.             esac
  521.  
  522.             sed -i "$L"'s/^\(\([^,]*,\)\{'"$(($INDEX -1))"'\}\)[^,]*/\1'"$NEW_VALUE"'/' $KK003_WORKBENCH/files/nvidia_smi_values.csv
  523.  
  524.         sed -i "$L"'s/^\(\([^,]*,\)\{'"$(($INDEX -1))"'\}\)[^,]*/\1'"$NEW_VALUE"'/' $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  525.         done
  526.     fi
  527.  
  528.     #### END of Arrange the output for the "temperature.gpu" nvidia-smi argument
  529.  
  530. done
  531.  
  532. # Insert the whole header line as the first one
  533. sed -i "1s/^/$CSV_HEADER\n/" $KK003_WORKBENCH/files/nvidia_smi_values.csv
  534.  
  535. sed -i "1s/^/$CSV_HEADER\n/" $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  536.  
  537. ###
  538. # End of block with custom my mods
  539. ###
  540.  
  541. }
  542.  
  543. function genoil_statics ()
  544. {
  545. #### Arrange the output for Genoil statics
  546.  
  547. if [[ "$ETHMINER_or_GENOIL_or_CLAYMORE" == "GENOIL" ]]; then
  548.        
  549.     # Check if Genoil is really running. Note that I have to search the full path as there is a few miners with the same exec's name
  550.     ps aux | grep -v grep | grep miner | grep -q "$KEY_GENOIL"
  551.     if  [[ $? -eq 0 ]]; then
  552.     GENOIL_IS_RUNNING="YES"
  553.     SLEEP_TIME_MODIFICATION_FILE=12         # Hope 12 segs is enough for screen to write enough new data and save the file
  554.     KNOWN_MINER_RUNNING=1               # 1= I know this miner
  555.     GENOIL_NUMBER_OF_HASHRATES_TO_SHOW=3        # Default 3, and I want to keep this value independent for each miner
  556.     echo ""
  557.     echo "It seems that GENOIL is running!!"
  558.  
  559.     # Clear the files
  560.     > $KK003_WORKBENCH/files/miner_statics.txt
  561.     > $KK003_WORKBENCH/files/miner_statics_log_file.txt
  562.  
  563.     # Insert Genoil's header
  564.     echo "" >> $KK003_WORKBENCH/files/miner_statics.txt
  565.         echo "-- Genoil mining  Information :" >> $KK003_WORKBENCH/files/miner_statics.txt
  566.  
  567.     echo "" >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  568.         echo "-- Genoil mining  Information :" >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  569.  
  570.     # Start log file for a few seconds and collect miner's statics
  571.     if [[ -f $KK003_WORKBENCH/kk003_screenlog.log ]]; then
  572.         > $KK003_WORKBENCH/kk003_screenlog.log                                  # Clear my log file
  573.     fi
  574.  
  575.     screen -dr miner -X logfile $KK003_WORKBENCH/kk003_screenlog.log                        # Don't want to overwrite the user's screen log file (screenlog.0 usually)
  576.     screen -dr miner -X log                                             # Start login miner
  577.     echo "Collecting statics from $ETHMINER_or_GENOIL_or_CLAYMORE for $SLEEP_TIME_MODIFICATION_FILE seconds..."
  578.     sleep $SLEEP_TIME_MODIFICATION_FILE                                     # Sleep while colleting data fron miner
  579.     screen -dr miner -X log                                             # Stop login miner
  580.  
  581.     # I don't see any valuable information than the total hashrate (at least I show a few)
  582.         # Surprise!!!, sed 's/ /\n/g' does not replace spaces for line breaks, but it does when executed from command line
  583.         TOTAL_HASHRATE=$(tail -60 $KK003_WORKBENCH/kk003_screenlog.log | grep "Mining on PoWhash" | cut -d":" -f4 |  cut -d" " -f2 | sed 's/ /\n/g' | tail -$GENOIL_NUMBER_OF_HASHRATES_TO_SHOW)
  584.  
  585.         if [[ -z $TOTAL_HASHRATE ]]; then
  586.             TOTAL_HASHRATE="No data available"
  587.         fi
  588.  
  589.         # Mount the little thing Genoil gives us
  590.         #
  591.         echo "Latest total hashrates : " >> $KK003_WORKBENCH/files/miner_statics.txt
  592.         echo $TOTAL_HASHRATE >> $KK003_WORKBENCH/files/miner_statics.txt
  593.  
  594.         echo "Latest total hashrates : " >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  595.         echo $TOTAL_HASHRATE >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  596.  
  597.     else
  598.     echo "Genoil does not seem to be running!!. Skiping this bit."
  599.     GENOIL_IS_RUNNING="NO"
  600.     fi
  601. fi
  602. }
  603.  
  604. function ethminer_statics ()
  605. {
  606. #### Arrange the output for ethminer statics
  607.  
  608. if [[ "$ETHMINER_or_GENOIL_or_CLAYMORE" == "ETHMINER" ]]; then
  609.  
  610.     # Check if ethminer is really running. Note that I have to search the full path as there is a few miners with the same exec's name
  611.     # Here I have to miner for ethminer
  612.     ps aux | grep -v grep | grep miner | grep -q "$KEY_ETHMINER_1"
  613.     RESP_CODE_1=$?
  614.     ps aux | grep -v grep | grep miner | grep -q "$KEY_ETHMINER_2"
  615.     RESP_CODE_2=$?
  616.     ps aux | grep -v grep | grep miner | grep -q "$KEY_ETHMINER_3"
  617.         RESP_CODE_3=$?
  618.  
  619.     if  [[ $RESP_CODE_1 -eq 0 || $RESP_CODE_2 -eq 0 || $RESP_CODE_3 -eq 0 ]]; then
  620.         ETHMINER_IS_RUNNING="YES"
  621.         KNOWN_MINER_RUNNING=1                   # 1= I known this miner
  622.         SLEEP_TIME_MODIFICATION_FILE=12         # Hope 12 segs is enough for screen to write enough new data and save the file
  623.         ETHMINER_NUMBER_OF_HASHRATES_TO_SHOW=3  # Default 3, and I want to keep this value independent for each miner
  624.         echo ""
  625.         echo "It seems that ETHMINER is running!!"
  626.  
  627.         # Clear the files
  628.         > $KK003_WORKBENCH/files/miner_statics.txt
  629.         > $KK003_WORKBENCH/files/miner_statics_log_file.txt
  630.  
  631.         # Insert ethminer's header
  632.         echo "" >> $KK003_WORKBENCH/files/miner_statics.txt
  633.         echo "-- Ethminer mining  Information :" >> $KK003_WORKBENCH/files/miner_statics.txt
  634.  
  635.         echo "" >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  636.         echo "-- Ethminer mining  Information :" >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  637.  
  638.     # Start log file for a few seconds and collect miner's statics
  639.         if [[ -f ~/kk003_screenlog.log ]]; then
  640.             > ~/kk003_screenlog.log                             # Clear my log file
  641.         fi
  642.  
  643.         screen -dr miner -X logfile ~/kk003_screenlog.log                                                               # Don't want to overwrite the user's screen log file (screenlog.0 usually)
  644.         screen -dr miner -X log                                                                                         # Start login miner
  645.         echo "Collecting statics from $ETHMINER_or_GENOIL_or_CLAYMORE for $SLEEP_TIME_MODIFICATION_FILE seconds..."
  646.         sleep $SLEEP_TIME_MODIFICATION_FILE                                                                             # Sleep while colleting data fron miner
  647.         screen -dr miner -X log                                                                                         # Stop login miner
  648.  
  649.         # I don't see any valuable information than the total hashrate (at least I show a few)
  650.         # Surprise!!!, sed 's/ /\n/g' does not replace spaces for line breaks, but it does when executed from command line
  651.         if [ $RESP_CODE_3 -eq 0 ]
  652.               then
  653.                 TOTAL_HASHRATE="$(tail -60 ~/nvoc_logs/screenlog.0 | grep "Speed" | cut -d " " -f 8 | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" |  tail -$ETHMINER_NUMBER_OF_HASHRATES_TO_SHOW)"
  654.            else
  655.                # For parsing earlier Etherminer log output
  656.                TOTAL_HASHRATE="$(tail -60 ~/nvoc_logs/screenlog.0 | grep "Mining on" | cut -d":" -f4 |  cut -d" " -f2 | sed 's/ /\n/g' | tail -$ETHMINER_NUMBER_OF_HASHRATES_TO_SHOW)"
  657.            fi
  658.        
  659.  
  660.        if [[ -z $TOTAL_HASHRATE ]]; then
  661.            TOTAL_HASHRATE="No data available"
  662.        fi
  663.  
  664.        # Mount the little thing ethminer gives us
  665.        #
  666.        echo "Latest total hashrates : " >> $KK003_WORKBENCH/files/miner_statics.txt
  667.        echo $TOTAL_HASHRATE >> $KK003_WORKBENCH/files/miner_statics.txt
  668.  
  669.        echo "Latest total hashrates : " >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  670.        echo $TOTAL_HASHRATE >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  671.  
  672.    else
  673.        echo "Ethminer does not seem to be running!!. Skiping this bit."
  674.        ETHMINER_IS_RUNNING="NO"
  675.    fi
  676. fi
  677.  
  678. }
  679.  
  680. function claymore_statics ()
  681. {
  682. #### Arrange the output for claymore statics
  683.  
  684. if [[ "$ETHMINER_or_GENOIL_or_CLAYMORE" == "CLAYMORE" ]]; then
  685.  
  686.    # Check if claymore is running
  687.    if  pgrep -x "$KEY_CLAYMORE" > /dev/null
  688.    then
  689.    CLAYMORE_IS_RUNNING="YES"
  690.    KNOWN_MINER_RUNNING=1               # 1= I known this miner
  691.    CLAYMORE_NUMBER_OF_HASHRATES_TO_SHOW=3          # Default 3, and I want to keep this value independent for each miner
  692.    echo
  693.    echo "It seems that Claymore is running!!"
  694.  
  695.    #
  696.    # Don't know why http://localhost:3333 access forces claymore's output to show up a line like:
  697.    # "GPU #XX: GeForce GTX 1060 3GB, 3013 MB available, 9 compute units, capability: 6.1", one line x gpu in the system
  698.     # Get the latest total hashrates
  699.     LATEST_TOTAL_HASHRATES=`curl -s http://localhost:3333 | sed '/Total/!d; /Speed/!d;' | cut -d":" -f 2 | sed 's/ //g' | cut -d"," -f1 | tail -n 3`
  700.     echo "$LATEST_TOTAL_HASHRATES" > $KK003_WORKBENCH/files/latest_total_hashrates.txt
  701.  
  702.     echo "$LATEST_TOTAL_HASHRATES" > $KK003_WORKBENCH/files/latest_total_hashrates_log_file.txt
  703.  
  704.     # I want total hasrates on one line ("sed" cannot do this job so I use "tr" instead)
  705.     tr '\n' ' ' < $KK003_WORKBENCH/files/latest_total_hashrates.txt > $KK003_WORKBENCH/files/latest_total_hashrates.txt.tmp
  706.     # Remove last character (wich is a ",")
  707. #   sed -i 's/.$//' $KK003_WORKBENCH/files/latest_total_hashrates.txt.tmp
  708.     echo >> $KK003_WORKBENCH/files/latest_total_hashrates.txt.tmp
  709.     mv $KK003_WORKBENCH/files/latest_total_hashrates.txt.tmp $KK003_WORKBENCH/files/latest_total_hashrates.txt
  710.  
  711.     cp $KK003_WORKBENCH/files/latest_total_hashrates.txt $KK003_WORKBENCH/files/latest_total_hashrates_log_file.txt
  712.  
  713.     # Insert header on top of file
  714.     sed -i "1s/^/Latest Total Hashrates :\n/" $KK003_WORKBENCH/files/latest_total_hashrates.txt
  715.  
  716.     sed -i "1s/^/Latest Total Hashrates :\n/" $KK003_WORKBENCH/files/latest_total_hashrates_log_file.txt
  717.  
  718.     # Get the indivual gpus hashrates and remove some charaters and spaces I don't want
  719.     GPUS_HASHRATES=`curl -s http://localhost:3333 | sed '/ETH: GPU/!d' | awk 'NR == 3' | sed 's/[^GPU]*\(GPU.*\)/\1/' | sed 's/ M/M/g' | sed 's/ G/G/g'`
  720.     # Make the string with individual hashrates vertical replacing "," for new line "\n" and removing the "GPUX" part
  721.     echo $GPUS_HASHRATES | sed "s/,/\n/g" | sed 's/^[^ ]* //' > $KK003_WORKBENCH/files/gpus_hashrate.txt
  722.  
  723.     echo $GPUS_HASHRATES | sed "s/,/\n/g" | sed 's/^[^ ]* //' > $KK003_WORKBENCH/files/gpus_hashrate_log_file.txt
  724.  
  725.     # Get total individual and rejected shares and for how long claymore is runing in one single string
  726.     # For 13 gpus should look something like this:
  727.     # Speed: 318.710 Mh/s, Total Shares: 15835(1233+1212+1226+1256+1216+1243+1228+1303+1220+1177+1237+1209+1222), Rejected: 0, Time: 55:32
  728.     #
  729.     SOME_GPUS_DATA_FROM_CLAYMORE=`curl -s http://localhost:3333 | sed '/ETH - Total/!d; /Speed/!d; /(/!d; /+/!d; s/[^Speed]*\(Speed.*\)/\1/;' | awk 'NR == 3'`
  730.  
  731.     # Get the total shares
  732.     TOTAL_SHARES=$(echo $SOME_GPUS_DATA_FROM_CLAYMORE | cut -d"(" -f 1 | cut -d" " -f 6)
  733.  
  734.     # Get the individual shares (I capture the values between the two parentheses)
  735.     # and replace the "+" signs for new line so I set the data vertical
  736.     echo $SOME_GPUS_DATA_FROM_CLAYMORE | sed 's/^.*(//; s/).*$//; s/+/\n/g' > $KK003_WORKBENCH/files/individual_shares.txt
  737.  
  738.     echo $SOME_GPUS_DATA_FROM_CLAYMORE | sed 's/^.*(//; s/).*$//; s/+/\n/g' > $KK003_WORKBENCH/files/individual_shares_log_file.txt
  739.  
  740.     # Get the rejected shares and remove de ","
  741.     REJECTED_SHARES=$(echo $SOME_GPUS_DATA_FROM_CLAYMORE | cut -d" " -f 8 | sed 's/,//')
  742.  
  743.     # How long claymore has been running?
  744.     TIME_CLAYMORE_IS_RUNNING=$(echo $SOME_GPUS_DATA_FROM_CLAYMORE | cut -d" " -f 10)
  745.  
  746.     # Creating a vertical gpux list
  747.     # If the file exists and is not empty clear it
  748.     if [[ -s $KK003_WORKBENCH/files/v_gpus.txt ]]; then
  749.             > $KK003_WORKBENCH/files/v_gpus.txt
  750.     fi
  751.  
  752.     if [[ -s $KK003_WORKBENCH/files/v_gpus_log_file.txt ]]; then
  753.         > $KK003_WORKBENCH/files/v_gpus_log_file.txt
  754.     fi
  755.  
  756.  
  757.     LINES_TO_ADD=$(($GPU_COUNT - 1))
  758.  
  759.         for ((L=0; L <= $LINES_TO_ADD ; L=L+1))
  760.         do
  761.  
  762.         case "$L" in
  763.  
  764.         0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 )
  765.         echo "GPU $L" >>$KK003_WORKBENCH/files/v_gpus.txt
  766.  
  767.         echo "GPU $L" >>$KK003_WORKBENCH/files/v_gpus_log_file.txt
  768.         ;;
  769.         *)
  770.         echo "GPU$L" >> $KK003_WORKBENCH/files/v_gpus.txt
  771.  
  772.         echo "GPU$L" >> $KK003_WORKBENCH/files/v_gpus_log_file.txt
  773.         ;;
  774.         esac
  775.         done
  776.  
  777.     # Paste the whole vertical thing together
  778.     paste -d, $KK003_WORKBENCH/files/v_gpus.txt $KK003_WORKBENCH/files/individual_shares.txt $KK003_WORKBENCH/files/gpus_hashrate.txt > $KK003_WORKBENCH/files/miner_statics.tmp
  779.  
  780.     paste -d, $KK003_WORKBENCH/files/v_gpus_log_file.txt $KK003_WORKBENCH/files/individual_shares_log_file.txt $KK003_WORKBENCH/files/gpus_hashrate_log_file.txt > $KK003_WORKBENCH/files/miner_statics_log_file.tmp
  781.  
  782.     # Mount the header field for claymore statics
  783.     CLAYMOR_STATICS_HEADER="Index, Shares, Hashrate"
  784.  
  785.     # Insert the whole header line as the first one
  786.     sed -i "1s/^/$CLAYMOR_STATICS_HEADER\n/" $KK003_WORKBENCH/files/miner_statics.tmp
  787.  
  788.     sed -i "1s/^/$CLAYMOR_STATICS_HEADER\n/" $KK003_WORKBENCH/files/miner_statics_log_file.tmp
  789.  
  790.     # Insert a new line on top
  791.     sed -i "1s/^/\n/" $KK003_WORKBENCH/files/miner_statics.tmp
  792.  
  793.     sed -i "1s/^/\n/" $KK003_WORKBENCH/files/miner_statics_log_file.tmp
  794.  
  795.     # Add the list of latest total hashrates and put it all togather
  796.     cat $KK003_WORKBENCH/files/latest_total_hashrates.txt $KK003_WORKBENCH/files/miner_statics.tmp > $KK003_WORKBENCH/files/miner_statics.txt
  797.  
  798.     cat $KK003_WORKBENCH/files/latest_total_hashrates_log_file.txt $KK003_WORKBENCH/files/miner_statics_log_file.tmp > $KK003_WORKBENCH/files/miner_statics_log_file.txt
  799.  
  800.     # Insert the Title for claymore mining information
  801.     sed -i "1s/^/-- Claymore mining  Information :\n/" $KK003_WORKBENCH/files/miner_statics.txt
  802.     sed -i "1s/^/\n/" $KK003_WORKBENCH/files/miner_statics.txt
  803.  
  804.     sed -i "1s/^/-- Claymore mining  Information :\n/" $KK003_WORKBENCH/files/miner_statics_log_file.txt
  805.     sed -i "1s/^/\n/" $KK003_WORKBENCH/files/miner_statics_log_file.txt
  806.  
  807.     # At the end of file add
  808.     echo " " >> $KK003_WORKBENCH/files/miner_statics.txt
  809.     echo "Total shares     :" $TOTAL_SHARES >> $KK003_WORKBENCH/files/miner_statics.txt
  810.     echo "Rejected shares  :" $REJECTED_SHARES >> $KK003_WORKBENCH/files/miner_statics.txt
  811.     echo "Claymore up time :" $TIME_CLAYMORE_IS_RUNNING >> $KK003_WORKBENCH/files/miner_statics.txt
  812.  
  813.     echo " " >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  814.     echo "Total shares     :" $TOTAL_SHARES >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  815.     echo "Rejected shares  :" $REJECTED_SHARES >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  816.     echo "Claymore up time :" $TIME_CLAYMORE_IS_RUNNING >> $KK003_WORKBENCH/files/miner_statics_log_file.txt
  817.  
  818.  
  819.     else
  820.     echo "Claymore does not seem to be running!!. Skiping this bit."
  821.     CLAYMORE_IS_RUNNING="NO"
  822.     fi
  823.  
  824. fi
  825.  
  826. #### END of Arrange the output for claymore statics
  827.  
  828. }
  829.  
  830. function system_info ()
  831. {
  832.  
  833. # Clear the file's content if exists and is not empty
  834. if [[ -s $KK003_WORKBENCH/files/system_data.txt ]]; then
  835.     > $KK003_WORKBENCH/files/system_data.txt
  836. fi
  837.  
  838. if [[ -s $KK003_WORKBENCH/files/system_data_log_file.txt ]]; then
  839.     > $KK003_WORKBENCH/files/system_data_log_file.txt
  840. fi
  841.  
  842. # Get the current public ip
  843. PUBLIC_IP=$(curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
  844.  
  845. if [[ -z "$PUBLIC_IP" ]]; then
  846.     echo "checkip.dyndns.org failure to return public ip. Trying on another service"
  847.  
  848.     PUBLIC_IP=`GET http://www.vermiip.es/  | grep "Tu IP p&uacute;blica es" | cut -d ':' -f2 | cut -d '<' -f1 | sed 's/ //'`
  849. fi
  850.  
  851. if [[ -z "$PUBLIC_IP" ]]; then
  852.     echo "checkip.dyndns.org and www.vermiip.es failure to return public ip. Trying on another service"
  853.  
  854.     PUBLIC_IP=`curl ifconfig.co`
  855. fi
  856.  
  857. if [[ -z "$PUBLIC_IP" ]]; then
  858.     echo "checkip.dyndns.org, www.vermiip.es and ifconfig.co failure to return public ip. There's no more servers available"
  859.     echo "Telegram won't send public ip."
  860.  
  861. else
  862.     # if var PUBLIC_IP is not empty check that I really have an ip address
  863.     valida_ip $PUBLIC_IP
  864.  
  865.     if [[ $ESTADO_TEST_IP -ne 0 ]]; then        # A non 0 value means bad ip syntax (usually server is sending text explaining an error)
  866.     PUBLIC_IP="No data available"
  867.     echo "Public ip's syntax is wrong:" $PUBLIC_IP
  868.     fi
  869. fi
  870.  
  871. # Get the current private ip/s
  872. PRIVATE_IP=$(hostname -I)
  873.  
  874. # Get the user custom name for the telegram reporting system
  875. if [[ -z "$CUSTOMIZE_SYSTEM_NAME" ]]; then
  876.     REPORTING="** $HOSTNAME Report **"
  877. elif [[ "$CUSTOMIZE_SYSTEM_NAME" == "PUBLIC_IP" && $ESTADO_TEST_IP -eq 0 ]]; then
  878.     REPORTING="** $PUBLIC_IP Report **"
  879. elif [[ "$CUSTOMIZE_SYSTEM_NAME" == "PRIVATE_IP" ]]; then
  880.     REPORTING="** $PRIVATE_IP Report **"
  881. else
  882.     REPORTING=$CUSTOMIZE_SYSTEM_NAME
  883. fi
  884.  
  885. echo "$REPORTING" >> $KK003_WORKBENCH/files/system_data.txt
  886. echo >> $KK003_WORKBENCH/files/system_data.txt
  887.  
  888. echo "$REPORTING" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  889. echo >> $KK003_WORKBENCH/files/system_data_log_file.txt
  890.  
  891. # Title for system information
  892. TITLE_SYSTEM_INFORMATION="-- System information :"
  893. echo "$TITLE_SYSTEM_INFORMATION" >> $KK003_WORKBENCH/files/system_data.txt
  894.  
  895. echo "$TITLE_SYSTEM_INFORMATION" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  896.  
  897. # Warning if system restarted
  898. if [[ "$SEND_WARNING_SYSTEM_RESTARTED" == "YES" ]]; then
  899.  
  900.     ELEMENT="W_SYSTEM_RESTARTED="
  901.  
  902.     if [[ $SYSTEM_RESTARTED -eq 1 ]]; then
  903.     sed -i "2s/^/WARNING: System just rebooted\n/" $KK003_WORKBENCH/files/system_data.txt
  904.  
  905.     sed -i "2s/^/WARNING: System just rebooted\n/" $KK003_WORKBENCH/files/system_data_log_file.txt
  906.  
  907.  
  908.     W_SYSTEM_RESTARTED=1
  909.  
  910.     # Write the warning count (+1) to corresponding variable
  911.     modify_warnings_file
  912.  
  913.     else    # if there is not warning then reset the value to "0"
  914.  
  915.     TOTAL_WARNINGS=0
  916.     sed -i "/$ELEMENT/ c$ELEMENT${TOTAL_WARNINGS}" $WARNINGS_FILE # I write the new value to file
  917.  
  918.     fi
  919. fi
  920.  
  921. # Warning if Xorg restarted
  922. if [[ "$SEND_WARNING_XORG_RESTARTED" == "YES" ]]; then
  923.  
  924.     ELEMENT="W_XORG_RESTARTED="
  925.  
  926.     if [[ $XORG_RESTARTED -eq 1 ]]; then
  927.     sed -i "2s/^/WARNING: XORG just restarted\n/" $KK003_WORKBENCH/files/system_data.txt
  928.  
  929.     sed -i "2s/^/WARNING: XORG just restarted\n/" $KK003_WORKBENCH/files/system_data_log_file.txt
  930.  
  931.     W_XORG_RESTARTED=1
  932.  
  933.     # Write the warning count (+1) to corresponding variable
  934.     modify_warnings_file
  935.  
  936.     else    # if there is not warning then reset the value to "0"
  937.  
  938.     TOTAL_WARNINGS=0
  939.     sed -i "/$ELEMENT/ c$ELEMENT${TOTAL_WARNINGS}" $WARNINGS_FILE # I write the new value to file
  940.  
  941.     fi
  942. fi
  943.  
  944. # Reboot required
  945. if [[ "$SEND_REBOOT_REQUIRED" == "YES" ]]; then
  946.     REBOOT_REQUIRED=$([ -f /var/run/reboot-required ] && echo "Yes!!!" || echo "No")
  947.     if [[ ! -z "$REBOOT_REQUIRED" ]]; then
  948.     echo "Reboot needed : $REBOOT_REQUIRED" >> $KK003_WORKBENCH/files/system_data.txt
  949.  
  950.     echo "Reboot needed : $REBOOT_REQUIRED" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  951.  
  952.     fi
  953. fi
  954.  
  955. # Worker name
  956. if [[ "$SEND_WORKERNAME" == "YES" ]]; then
  957.     echo -n "Worker : " >> $KK003_WORKBENCH/files/system_data.txt
  958.     echo "$WORKERNAME" >> $KK003_WORKBENCH/files/system_data.txt
  959.  
  960.     echo -n "Worker        : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  961.     echo "$WORKERNAME" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  962. fi
  963.  
  964. # Mining coin and auto-switch running check
  965. if [[ "$COIN" == "SALFTER_NICEHASH_PROFIT_SWITCHING" ]]; then
  966.     AUTO_SWITCH_ON="YES"
  967.     AUTO_SWITCH_NAME="Salfter Nicehash Auto Switch"
  968.     CURRENT_COIN=$(head -n 1 ~/current-profit)
  969.     MINING_HISTORY=$(tail -n 5 ~/algo_log | awk '{print $0}')
  970. fi
  971.  
  972. if  [[ "$COIN" == "SALFTER_MPH_PROFIT_SWITCHING" ]]; then
  973.     AUTO_SWITCH_ON="YES"
  974.     AUTO_SWITCH_NAME="Salfter MPH Auto Switch"    
  975.     CURRENT_COIN=$(head -n 1 ~/current-profit)
  976.     MINING_HISTORY=$(tail -n 5 ~/algo_log | awk '{print $0}')
  977. fi
  978.  
  979. if [[ $PAPAMPI_WTM_AUTO_SWITCH == "YES" ]]; then                # If user has selected "YES" here does not means WTM is running, so lets check
  980.     # Check if WTM is running
  981.     ps aux | grep -v grep | grep -q PAPAMPI_WTM
  982.     if [[ $? -eq 0 ]]; then                         # WTM is running and I take the opportunity to collect some data
  983.     AUTO_SWITCH_ON="YES"
  984.     AUTO_SWITCH_NAME="Papampi WTM Auto Switch"
  985.     CURRENT_COIN=$COIN
  986.     MINING_HISTORY=$(cat ~/8_wtmautoswitchlog | grep 'Mining switched' | tail -n 10 | awk '{print $0}')
  987.     WTM_PROFIT=$(cat ~/WTM_current_profit | awk '{print $0,"%"}')
  988.     WTM_REV=$(cat ~/WTM_current_revenue | awk '{print $0}')
  989.     fi
  990. fi
  991.  
  992. if [[ $AUTO_SWITCH_ON != "YES" ]]; then
  993.     CURRENT_COIN=$COIN
  994. fi
  995.    
  996. if [[ "$SEND_MINING_COIN" == "YES" ]]; then
  997.     echo -n "Coin : " >> $KK003_WORKBENCH/files/system_data.txt
  998.     echo "$CURRENT_COIN" >> $KK003_WORKBENCH/files/system_data.txt
  999.  
  1000.     echo -n "Coin          : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1001.     echo "$CURRENT_COIN" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1002. fi
  1003.  
  1004. # Mining address
  1005. # There is not a unique varible in nvoc that defines de mining address so source from 1bash do not work
  1006. # Here I take my chances and I'll try to get the mining address directly from the process list.
  1007. # The address field should be the larger string so that is what I will go for.
  1008. # What I do is replace spaces for new lines (\n) so I split the hole thing in lines.
  1009. # Then remove the line that contains "/home/m1" because is/may be a large one and I know for sure this is not the mining address.
  1010. # I get the larger line with the help of "awk"
  1011. # If there is a dot "." or lash "/" I remove all chars from it to the end of line because as long as I know address don't have a dot and usually not a slash (remove the worker name)
  1012. # Other approach would be a search for "0x" but I'm not sure if that is a universal solution
  1013. if [[ "$SEND_MINING_ADDRESS" == "YES" ]]; then
  1014.     MINING_ADDRESS=$(ps aux | grep -v grep | grep " miner " | sed 's/ /\n/g' | sed '/\home\/m1/d' | awk ' { if ( length > x ) { x = length; y = $0 } }END{ print y }' | sed 's/\..*//' | sed 's/\/.*//')
  1015.     echo -n "Address : " >> $KK003_WORKBENCH/files/system_data.txt
  1016.  
  1017.     echo -n "Address       : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1018.  
  1019.     if [[ "$MASK_MINING_ADDRESS" == "YES" ]]; then
  1020.     ADDRESS_HEAD=$(echo "$MINING_ADDRESS" | cut -c1-5)  # Get the first 5 chars
  1021.     ADDRESS_TAIL=$(echo -n "$MINING_ADDRESS" | tail -c 5)   # Get the last 5 chars
  1022.     MASKED_ADDRESS=$ADDRESS_HEAD"*****"$ADDRESS_TAIL    # Paste the hole thing
  1023.     echo "$MASKED_ADDRESS" >> $KK003_WORKBENCH/files/system_data.txt
  1024.  
  1025.     echo "$MASKED_ADDRESS" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1026.  
  1027.     else
  1028.     echo "$MINING_ADDRESS" >> $KK003_WORKBENCH/files/system_data.txt
  1029.  
  1030.     echo "$MINING_ADDRESS" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1031.     fi
  1032. fi
  1033.  
  1034. # Miner exec name
  1035. # There is not a unique varible in nvoc that defines path or miner so source from 1bash do not work
  1036. MINER_EXEC_PATH=$(ps aux | grep -v grep | grep " miner " | sed 's/ /\n/g' | grep "/home/m1/")
  1037. MINER_NAME=$(ps aux | grep -v grep | grep " miner " | sed 's/ /\n/g' | grep "/home/m1/" | grep -oE "[^/]+$")
  1038. if [[ "$SEND_MINER_EXEC_NAME" == "YES" && "$SEND_MINER_EXEC_PATH" == "YES" ]]; then
  1039.     echo -n "Exec : " >> $KK003_WORKBENCH/files/system_data.txt
  1040.     echo "$MINER_EXEC_PATH" >> $KK003_WORKBENCH/files/system_data.txt
  1041.  
  1042.     echo -n "Exec          : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1043.     echo "$MINER_EXEC_PATH" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1044.  
  1045. elif [[ "$SEND_MINER_EXEC_NAME" == "YES" ]]; then
  1046.     echo -n "Exec : " >> $KK003_WORKBENCH/files/system_data.txt
  1047.     echo "$MINER_NAME" >> $KK003_WORKBENCH/files/system_data.txt
  1048.  
  1049.     echo -n "Exec          : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1050.     echo "$MINER_NAME" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1051. fi
  1052.  
  1053. # Miner up time
  1054. if [[ "$SEND_MINER_UP_TIME" == "YES" ]]; then
  1055.     ps aux | grep -v grep | grep -q " miner "                       # Check if procces exists
  1056.     if [[ $? -eq 0 ]]; then
  1057.     MINER_UP_TIME=$(ps -o etime= -p $(pgrep -f miner) | tail -n 1 | sed 's/ //g')
  1058.     else
  1059.     MINER_UP_TIME="No data available"
  1060.     fi
  1061.  
  1062.     echo "Miner uptime : $MINER_UP_TIME" >> $KK003_WORKBENCH/files/system_data.txt
  1063.  
  1064.     echo "Miner uptime  : $MINER_UP_TIME" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1065. fi
  1066.  
  1067. # Ips
  1068. # Private ip
  1069. if [[ "$SEND_PRIVATE_IP" == "YES" ]]; then
  1070.     echo -n "Private Ip : " >> $KK003_WORKBENCH/files/system_data.txt
  1071.     echo $PRIVATE_IP >> $KK003_WORKBENCH/files/system_data.txt
  1072.  
  1073.     echo -n "Private Ip    : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1074.     echo $PRIVATE_IP >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1075. fi
  1076.  
  1077. ## Public ip
  1078. #
  1079. # Check if public ip has been changed
  1080. ELEMENT="W_PUBLIC_IP_CHANGED="
  1081. LAST_PUBLIC_IP=$(cat $DATA_FILE | grep "PUBLIC_IP=" | cut -d"=" -f2)            # Find the line for public ip and get the last known ip
  1082.  
  1083. if [[ ! -z "$PUBLIC_IP" && $ESTADO_TEST_IP -eq 0 ]]; then               # If I have public ip with right syntax then
  1084.  
  1085.     if [[ "$LAST_PUBLIC_IP" == "" ]]; then                      # If it's empty
  1086.     echo "PUBLIC_IP=$PUBLIC_IP" >> $DATA_FILE                   # I write the public ip
  1087.     fi
  1088.  
  1089.     if [[ "$LAST_PUBLIC_IP" != "" && "$LAST_PUBLIC_IP" != "$PUBLIC_IP" ]]; then     # In this case ip has changed
  1090.     sed -i "/PUBLIC_IP=/ cPUBLIC_IP=${PUBLIC_IP}" $DATA_FILE            # Update public ip to file
  1091.  
  1092.     sed -i "2s/^/WARNING: Public ip has changed from $LAST_PUBLIC_IP to $PUBLIC_IP\n/" $KK003_WORKBENCH/files/system_data.txt
  1093.  
  1094.     sed -i "2s/^/WARNING: Public ip has changed from $LAST_PUBLIC_IP to $PUBLIC_IP\n/" $KK003_WORKBENCH/files/system_data_log_file.txt
  1095.  
  1096.     W_PUBLIC_IP_CHANGED=1
  1097.  
  1098.     # Write the warning count (+1) to warning variable
  1099.         modify_warnings_file
  1100.  
  1101.     else # if there is not warning then reset the value to "0"
  1102.  
  1103.     TOTAL_WARNINGS=0
  1104.     sed -i "/$ELEMENT/ c$ELEMENT${TOTAL_WARNINGS}" $WARNINGS_FILE # I write the new value to file
  1105.     fi
  1106.  
  1107. else    # Probably network and/or internet connection is down
  1108.  
  1109.     echo "WARNING: It seems that, internet connection is down." >> $KK003_WORKBENCH/files/system_data.txt
  1110.  
  1111.     echo "WARNING: It seems that, internet connection is down." >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1112.  
  1113. fi
  1114.  
  1115. # Send public ip
  1116. if [[ "$SEND_PUBLIC_IP" == "YES" ]]; then
  1117.     echo -n "Public Ip : " >> $KK003_WORKBENCH/files/system_data.txt
  1118.  
  1119.     echo -n "Public Ip     : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1120.  
  1121.     if [[ "$MASK_PUBLIC_IP" == "YES" ]]; then
  1122.     IP_ADDRESS_HEAD=$(echo "$PUBLIC_IP" | cut -c1-3)        # Get the first 3 chars
  1123.     IP_ADDRESS_TAIL=$(echo -n "$PUBLIC_IP" | tail -c 3)     # Get the last 3 chars
  1124.     IP_MASKED_ADDRESS=$IP_ADDRESS_HEAD"*****"$IP_ADDRESS_TAIL   # Paste the hole thing
  1125.     echo $IP_MASKED_ADDRESS >> $KK003_WORKBENCH/files/system_data.txt
  1126.  
  1127.     echo $IP_MASKED_ADDRESS >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1128.     else
  1129.     echo $PUBLIC_IP >> $KK003_WORKBENCH/files/system_data.txt
  1130.  
  1131.     echo $PUBLIC_IP >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1132.     fi
  1133. fi
  1134.  
  1135.  
  1136. # Send sshd port
  1137. if [[ "$SEND_SSHD_PORT" == "YES" ]]; then
  1138.     # lsoft needs sudo. nmap IS NOT installed for default in nvoc system
  1139.     SSHD_PORT=$(sudo lsof -iTCP -sTCP:LISTEN | grep IPv4 | grep sshd | cut -d":" -f2 | sed "s/ .*$//")
  1140.  
  1141.     echo "Sshd port : $SSHD_PORT" >> $KK003_WORKBENCH/files/system_data.txt
  1142.  
  1143.     echo "Sshd port     : $SSHD_PORT" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1144. fi
  1145.  
  1146. # Up since
  1147. if [[ "$SEND_UP_SINCE" == "YES" ]]; then
  1148.     echo -n "Up since : " >> $KK003_WORKBENCH/files/system_data.txt
  1149.     uptime -s >> $KK003_WORKBENCH/files/system_data.txt
  1150.  
  1151.     echo -n "Up since      : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1152.     uptime -s >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1153. fi
  1154.  
  1155. # Up time
  1156. if [[ "$SEND_UP_TIME" == "YES" ]]; then
  1157.     echo -n "Time up : " >> $KK003_WORKBENCH/files/system_data.txt
  1158.     uptime -p | sed 's/up //' >> $KK003_WORKBENCH/files/system_data.txt
  1159.  
  1160.     echo -n "Time up       : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1161.     uptime -p | sed 's/up //' >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1162. fi
  1163.  
  1164. # Ram
  1165. if [[ "$SEND_RAM" == "YES" ]]; then
  1166.     RAM=$(grep MemTotal /proc/meminfo | awk '{print $2}' | xargs -I {} echo "scale=2; {}/1024^2" | bc )
  1167.  
  1168.     echo -n "Ram : " >> $KK003_WORKBENCH/files/system_data.txt
  1169.     echo "$RAM Gb"  >> $KK003_WORKBENCH/files/system_data.txt
  1170.  
  1171.     echo -n "Ram           : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1172.     echo "$RAM Gb"  >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1173. fi
  1174.  
  1175. # Cpu model
  1176. CPU_MODEL=$(cat /proc/cpuinfo | grep 'model name' | uniq | cut -d":" -f2)
  1177. # Number of cores
  1178. NUMBER_OF_CORES=$(nproc)
  1179.  
  1180. if [[ "$SEND_CPU_MODEL" == "YES" ]]; then
  1181.     CPU_SPECS="$CPU_MODEL (cores = $NUMBER_OF_CORES)"
  1182.  
  1183.     echo -n "Cpu : " >> $KK003_WORKBENCH/files/system_data.txt
  1184.     echo $CPU_SPECS >> $KK003_WORKBENCH/files/system_data.txt
  1185.  
  1186.     echo -n "Cpu           : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1187.     echo $CPU_SPECS >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1188. fi
  1189.  
  1190. # Load average
  1191. if [[ "$SEND_WARNING_LOAD_AVERAGE" == "YES" ]]; then
  1192.  
  1193.     ELEMENT="W_HIGH_CPU_LOAD_AVERAGE="
  1194.  
  1195.     echo "Load average : " >> $KK003_WORKBENCH/files/system_data.txt
  1196.  
  1197.     echo "Load average  : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1198.  
  1199.     # Get cpu load average for 1, 5 and 15 minutes in a single string and remove spaces
  1200.     AVERAGES=$(uptime | sed 's/.*: //g; s/ //g')
  1201.  
  1202.     AVERAGE_1=$(echo $AVERAGES | cut -d"," -f1)
  1203.     AVERAGE_5=$(echo $AVERAGES | cut -d"," -f2)
  1204.     AVERAGE_15=$(echo $AVERAGES | cut -d"," -f3)
  1205.  
  1206.     # Returns "1" when the load average is equal or greater than number of cpu cores
  1207.     # else returns "0"
  1208.     AVERAGE_1_HIGH=$(echo "$AVERAGE_1 >= $NUMBER_OF_CORES" | bc -l )
  1209.     AVERAGE_5_HIGH=$(echo "$AVERAGE_5 >= $NUMBER_OF_CORES" | bc -l )
  1210.     AVERAGE_15_HIGH=$(echo "$AVERAGE_15 >= $NUMBER_OF_CORES" | bc -l )
  1211.  
  1212.     # Check if load average is to high
  1213.     if [[ $AVERAGE_1_HIGH -eq 1 ]]; then
  1214.     echo "$AVERAGE_1 (last minute) WARNING: cpu load average too high" >> $KK003_WORKBENCH/files/system_data.txt
  1215.  
  1216.     echo "$AVERAGE_1 (last minute) WARNING: cpu load average too high" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1217.  
  1218.     W_HIGH_CPU_LOAD_AVERAGE=1
  1219.     else
  1220.     echo "$AVERAGE_1 (last minute)" >> $KK003_WORKBENCH/files/system_data.txt
  1221.  
  1222.     echo "$AVERAGE_1 (last minute)" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1223.     fi
  1224.  
  1225.     if [[ $AVERAGE_5_HIGH -eq 1 ]]; then
  1226.     echo "$AVERAGE_5 (last 5 minutes) WARNING: cpu load average too high" >> $KK003_WORKBENCH/files/system_data.txt
  1227.  
  1228.     echo "$AVERAGE_5 (last 5 minutes) WARNING: cpu load average too high" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1229.  
  1230.     W_HIGH_CPU_LOAD_AVERAGE=1
  1231.     else
  1232.     echo "$AVERAGE_5 (last 5 minutes)" >> $KK003_WORKBENCH/files/system_data.txt
  1233.  
  1234.     echo "$AVERAGE_5 (last 5 minutes)" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1235.     fi
  1236.  
  1237.     if [[ $AVERAGE_15_HIGH -eq 1 ]]; then
  1238.     echo "$AVERAGE_15 (last 15 minutes) WARNING: cpu load average too high" >> $KK003_WORKBENCH/files/system_data.txt
  1239.  
  1240.     echo "$AVERAGE_15 (last 15 minutes) WARNING: cpu load average too high" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1241.  
  1242.     W_HIGH_CPU_LOAD_AVERAGE=1
  1243.     else
  1244.     echo "$AVERAGE_15 (last 15 minutes)" >> $KK003_WORKBENCH/files/system_data.txt
  1245.  
  1246.     echo "$AVERAGE_15 (last 15 minutes)" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1247.     fi
  1248.  
  1249.     if [[ $AVERAGE_1_HIGH -eq 1 || $AVERAGE_5_HIGH -eq 1 || $AVERAGE_15_HIGH -eq 1 ]]; then
  1250.     sed -i "2s/^/WARNING: Cpu load average too high!!!\n/" $KK003_WORKBENCH/files/system_data.txt
  1251.  
  1252.         sed -i "2s/^/WARNING: Cpu load average too high!!!\n/" $KK003_WORKBENCH/files/system_data_log_file.txt
  1253.  
  1254.     # Write the warning count (+1) to warning variable
  1255.     modify_warnings_file
  1256.  
  1257.  
  1258.     else    # if there is not warning then reset the value to "0"
  1259.  
  1260.     TOTAL_WARNINGS=0
  1261.         sed -i "/$ELEMENT/ c$ELEMENT${TOTAL_WARNINGS}" $WARNINGS_FILE # I write the new value to file
  1262.  
  1263.     fi
  1264. fi
  1265.  
  1266. # who is logged in the system
  1267. if [[ "$SEND_LOGGED_USERS" == "YES" ]]; then
  1268.     echo "Logged in users : " >> $KK003_WORKBENCH/files/system_data.txt
  1269.     who >> $KK003_WORKBENCH/files/system_data.txt
  1270.  
  1271.     echo "Logged in users : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1272.     who >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1273. fi
  1274.  
  1275. # Check if var DISPLAY has a value
  1276. if [[ "$SEND_WARNING_CHECK_DISPLAY" == "YES" ]]; then
  1277.  
  1278.     ELEMENT="W_WARNING_DISPLAY="
  1279.  
  1280.     DISPLAY=$(who | grep "(:") # Here I check if there is an open sesion on any display (like display 0). If not may be the x server is down
  1281.  
  1282.     if [[ -z "$DISPLAY" ]]; then
  1283.  
  1284.     sed -i "2s/^/WARNING: Not a user logged in any Display. May be X server is NOT running\n/" $KK003_WORKBENCH/files/system_data.txt
  1285.  
  1286.     sed -i "2s/^/WARNING: Not a user logged in any Display. May be X server is NOT running\n/" $KK003_WORKBENCH/files/system_data_log_file.txt
  1287.  
  1288.     W_WARNING_DISPLAY=1
  1289.  
  1290.     # Write the warning count (+1) to warning variable
  1291.     modify_warnings_file
  1292.  
  1293.  
  1294.     else        # if there is not warning then reset the value to "0"
  1295.  
  1296.         TOTAL_WARNINGS=0
  1297.         sed -i "/$ELEMENT/ c$ELEMENT${TOTAL_WARNINGS}" $WARNINGS_FILE # I write the new value to file
  1298.  
  1299.     fi
  1300. fi
  1301. echo "" >> $KK003_WORKBENCH/files/system_data.txt
  1302.  
  1303. echo "" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1304.  
  1305.  
  1306. # Ensure system has all gpus
  1307. echo "System   is  reporting : $GPU_COUNT Gpus" >> $KK003_WORKBENCH/files/system_data.txt
  1308. echo "System should have  : $NUMBER_GPUS_FOR_SURE Gpus" >> $KK003_WORKBENCH/files/system_data.txt
  1309.  
  1310. echo "System is reporting : $GPU_COUNT Gpus" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1311. echo "System should have  : $NUMBER_GPUS_FOR_SURE Gpus" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1312.  
  1313. ELEMENT="W_GPU_COUNT="
  1314.  
  1315. if [[ $GPU_COUNT -ne $NUMBER_GPUS_FOR_SURE ]]; then
  1316.     sed -i "2s/^/WARNING: THERE MAY BE A PROBLEM; we should have $NUMBER_GPUS_FOR_SURE GPUs but nvidia-smi reports $GPU_COUNT\n/" $KK003_WORKBENCH/files/system_data.txt
  1317.  
  1318.     sed -i "2s/^/WARNING: THERE MAY BE A PROBLEM; we should have $NUMBER_GPUS_FOR_SURE GPUs but nvidia-smi reports $GPU_COUNT\n/" $KK003_WORKBENCH/files/system_data_log_file.txt
  1319.  
  1320.     W_GPU_COUNT=1
  1321.  
  1322.     # Write the warning count (+1) to warning variable
  1323.     modify_warnings_file
  1324.  
  1325. else        # if there is not warning then reset the value to "0"
  1326.  
  1327.     TOTAL_WARNINGS=0
  1328.     sed -i "/$ELEMENT/ c$ELEMENT${TOTAL_WARNINGS}" $WARNINGS_FILE # I write the new value to file
  1329. fi
  1330.  
  1331. # If any auto-switch running show the info
  1332. if [[ "$AUTO_SWITCH_ON" == "YES" ]]; then
  1333.     TITLE_AUTO_SWITCH="-- "$AUTO_SWITCH_NAME" :"
  1334.     echo "$TITLE_AUTO_SWITCH" >> $KK003_WORKBENCH/files/system_data.txt
  1335.     echo "" >> $KK003_WORKBENCH/files/system_data.txt
  1336.  
  1337.     echo "$TITLE_AUTO_SWITCH" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1338.     echo "" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1339.  
  1340.     if [[ "$AUTO_SWITCH_NAME" == "Salfter Nicehash Auto Switch" || "$AUTO_SWITCH_NAME" == "Salfter MPH Auto Switch" ]]; then
  1341.     echo -n "Coin : " >> $KK003_WORKBENCH/files/system_data.txt
  1342.     echo "$CURRENT_COIN" >> $KK003_WORKBENCH/files/system_data.txt
  1343.  
  1344.     echo -n "Coin          : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1345.     echo "$CURRENT_COIN" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1346.  
  1347.     echo "M. History : " >> $KK003_WORKBENCH/files/system_data.txt
  1348.         echo "$MINING_HISTORY" >> $KK003_WORKBENCH/files/system_data.txt
  1349.  
  1350.         echo -n "M. History    : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1351.         echo "$MINING_HISTORY" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1352.     fi
  1353.  
  1354.     if [[ "$AUTO_SWITCH_NAME" == "Papampi WTM Auto Switch" ]]; then
  1355.     echo -n "Coin : " >> $KK003_WORKBENCH/files/system_data.txt
  1356.         echo "$CURRENT_COIN" >> $KK003_WORKBENCH/files/system_data.txt
  1357.  
  1358.         echo -n "Coin          : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1359.         echo "$CURRENT_COIN" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1360.  
  1361.         echo "M. History : " >> $KK003_WORKBENCH/files/system_data.txt
  1362.         echo "$MINING_HISTORY" >> $KK003_WORKBENCH/files/system_data.txt
  1363.  
  1364.         echo -n "M. History    : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1365.         echo "$MINING_HISTORY" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1366.  
  1367.     echo -n "Profit : " >> $KK003_WORKBENCH/files/system_data.txt
  1368.     echo "$WTM_PROFIT" >> $KK003_WORKBENCH/files/system_data.txt
  1369.  
  1370.     echo -n "Profit        : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1371.     echo "$WTM_PROFIT" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1372.  
  1373.     echo -n "Revenue : " >> $KK003_WORKBENCH/files/system_data.txt
  1374.         echo "$WTM_REV" >> $KK003_WORKBENCH/files/system_data.txt
  1375.  
  1376.         echo -n "Revenue       : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1377.         echo "$WTM_REV" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1378.  
  1379.     WTM_AVERAGE_REV=$(awk '{s+=$1}END{print "",s/NR}' RS="\n"  /home/m1/WTM_24H_REV)
  1380.     echo "Revenue Average (24h): " >> $KK003_WORKBENCH/files/system_data.txt
  1381.         echo "$WTM_AVERAGE_REV" >> $KK003_WORKBENCH/files/system_data.txt
  1382.  
  1383.         echo -n "Revenue Average (24h): " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1384.         echo "$WTM_AVERAGE_REV" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1385.  
  1386.     BTC_PRICE=$(cat /home/m1/WTM_BTC_EXCHANGE_RATE | awk '{printf("%d\n",$1 + 0)}' )
  1387.     echo "BTC Price: " >> $KK003_WORKBENCH/files/system_data.txt
  1388.         echo "$BTC_PRICE" >> $KK003_WORKBENCH/files/system_data.txt
  1389.  
  1390.         echo -n "BTC Price     : " >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1391.         echo "$BTC_PRICE" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1392.     fi
  1393. fi
  1394.  
  1395.  
  1396. echo "" >> $KK003_WORKBENCH/files/system_data.txt
  1397.  
  1398. echo "" >> $KK003_WORKBENCH/files/system_data_log_file.txt
  1399.  
  1400. }
  1401.  
  1402. function regenerate_warnings_file ()
  1403. {
  1404. # Regenerates the warnings file
  1405. # I must find out:
  1406. # 1. First time the script has been executed but system not just restarted?
  1407. # 2. System has just been restarted?
  1408. # So i avoid the need to restart the system for this script to work properly if ran for first time
  1409. if [[ ! -f "$WARNINGS_FILE" ]]; then
  1410.     touch $WARNINGS_FILE
  1411.     insert_vars_warnings_file
  1412. fi
  1413.  
  1414. if [[ -f "$WARNINGS_FILE" && $SYSTEM_RESTARTED -eq 1 || -f "$WARNINGS_FILE" && $XORG_RESTARTED -eq 1 ]]; then # 1=system/Xorg just restarted
  1415.     > $WARNINGS_FILE
  1416.     insert_vars_warnings_file
  1417. fi
  1418.  
  1419. }
  1420.  
  1421. function modify_warnings_file ()
  1422. {
  1423. # Get the value in warnings file and check if above define user limit to send a telegram (var "ON_WARNING_NUMBER_TELEGRAMS_TO_SEND", default is 3)
  1424. VALUE=$(cat $WARNINGS_FILE | grep "$ELEMENT" | cut -d"=" -f2)
  1425. TOTAL_WARNINGS=$(($VALUE + 1))
  1426. sed -i "/$ELEMENT/ c$ELEMENT${TOTAL_WARNINGS}" $WARNINGS_FILE       # I write the new value to file
  1427.  
  1428. if [[ $TOTAL_WARNINGS -le $ON_WARNING_NUMBER_TELEGRAMS_TO_SEND ]]; then # if true I send a telegram
  1429.     SEND_WARNING=1
  1430. fi
  1431. }
  1432.  
  1433. function insert_vars_warnings_file ()
  1434. {
  1435. # Insert the warning vars to the warnings file
  1436. echo "W_SYSTEM_RESTARTED=0" >> $WARNINGS_FILE
  1437. echo "W_XORG_RESTARTED=0" >> $WARNINGS_FILE
  1438. echo "W_HIGH_CPU_LOAD_AVERAGE=0" >> $WARNINGS_FILE
  1439. echo "W_WARNING_DISPLAY=0" >> $WARNINGS_FILE
  1440. echo "W_GPU_COUNT=0" >> $WARNINGS_FILE
  1441. echo "W_BAD_USER_SEND_TIME_FORMAT=0" >> $WARNINGS_FILE
  1442. echo "W_PUBLIC_IP_CHANGED=0" >> $WARNINGS_FILE
  1443. #echo "W_PRIVATE_IP_CHANGED=0" >> $WARNINGS_FILE
  1444. echo "W_INTERVAL_SEND_TELEGRAM_CHANGED=0" >> $WARNINGS_FILE
  1445. }
  1446.  
  1447. function time_of_day_to_send ()
  1448. {
  1449. # Check if is time to send telegram according to user customization
  1450. #
  1451. # This var allows you to log the time calculation related to user custom times to send a telegram
  1452. SHOW_TIME_CALCULATIONS_IN_LOG_FILE=0    #1 or 0, 1=log time calculations, 0=No
  1453.  
  1454. TIME_NOW_IN_SECONDS=$(date +%s)
  1455. TIME_NOW_IN_DATE_FORMAT=$(date --date @$TIME_NOW_IN_SECONDS)
  1456. USER_CUSTOM_TIME_IN_SECONDS=$(date -d $SEND_TIME +%s)
  1457. USER_CUSTOM_TIME_IN_DATE_FORMAT=$(date --date @$USER_CUSTOM_TIME_IN_SECONDS)
  1458. MY_INTERVAL_IN_SECONDS=$(($TIME_NOW_IN_SECONDS + $ONE_BASH_TELEGRAM_INTERVAL_IN_SECONDS))
  1459. MY_INTERVAL_IN_DATE_FORMAT=$(date --date @$MY_INTERVAL_IN_SECONDS)
  1460. SECONDS_TO_SEND_USER_CUSTOM_TELEGRAM=$(($USER_CUSTOM_TIME_IN_SECONDS - $TIME_NOW_IN_SECONDS))
  1461. TIME_TO_SEND_USER_CUSTOM_TELEGRAM_IN_DATE_FORMAT=$(date --date @$SECONDS_TO_SEND_USER_CUSTOM_TELEGRAM)
  1462.  
  1463. if [[ $SHOW_TIME_CALCULATIONS_IN_LOG_FILE -eq 1 ]]; then    # The script does not send a custom time telegram at 00:00
  1464.                                 # I'll use this info some time if a decide to pacth this issue
  1465.     echo "ONE_BASH_TELEGRAM_INTERVAL_IN_SECONDS: " $ONE_BASH_TELEGRAM_INTERVAL_IN_SECONDS
  1466.     echo "TIME_NOW_IN_SECONDS / Time now in date format : $TIME_NOW_IN_SECONDS / $TIME_NOW_IN_DATE_FORMAT"
  1467.     echo "User custom send time / USER_CUSTOM_TIME_IN_SECONDS / User custom time date format : $SEND_TIME / $USER_CUSTOM_TIME_IN_SECONDS / $USER_CUSTOM_TIME_IN_DATE_FORMAT"
  1468.     echo "MY_INTERVAL_IN_SECONDS / Time next check in date format : $MY_INTERVAL_IN_SECONDS / $MY_INTERVAL_IN_DATE_FORMAT"
  1469.     echo "SECONDS_TO_SEND_USER_CUSTOM_TELEGRAM / TIME_TO_SEND_USER_CUSTOM_TELEGRAM_IN_DATE_FORMAT : $SECONDS_TO_SEND_USER_CUSTOM_TELEGRAM / $TIME_TO_SEND_USER_CUSTOM_TELEGRAM_IN_DATE_FORMAT"
  1470. fi
  1471.  
  1472. if [[ $TIME_NOW_IN_SECONDS -gt $USER_CUSTOM_TIME_IN_SECONDS ]]; then
  1473. #    echo "La hora actual es mayor que la de la alerta"
  1474. #    echo
  1475.     return
  1476. fi
  1477.  
  1478. if [[ $MY_INTERVAL_IN_SECONDS -ge $USER_CUSTOM_TIME_IN_SECONDS ]]; then
  1479.     SLEEP_TIME=$(($USER_CUSTOM_TIME_IN_SECONDS - $TIME_NOW_IN_SECONDS))
  1480.     echo "Sleeping $SLEEP_TIME seconds before send user custom time telegram"
  1481.     SEND_USER_CUSTOM_TELEGRAM=1
  1482.     sleep $SLEEP_TIME
  1483. fi
  1484.  
  1485. }
  1486.  
  1487.  
  1488. function custom_telegram_ordered_by_user ()
  1489. {
  1490. if [[ $USE_USER_CUSTOM_TIMES_TELEGRAM -eq 1 ]]; then
  1491.     SEND_USER_CUSTOM_TELEGRAM=0
  1492.     WRONG_TIME_FORMAT=""
  1493.     ELEMENT="W_BAD_USER_SEND_TIME_FORMAT="
  1494.     CHECK_TIME_REGEX="^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$"
  1495.  
  1496.     # First must check that array is not empty
  1497.     TIMES_LENGTH=${#USER_CUSTOM_TIMES_TO_SEND_TELEGRAM[@]} # Get number of elements
  1498.  
  1499.     if [[ $TIMES_LENGTH -ne 0 ]]; then # Is not empty
  1500.  
  1501.     # In this loop I do:
  1502.     # 1. Time format is right?
  1503.     # 2. Calculate if it's the right time to send the user custom time telegram
  1504.     for ((INDEX=0; INDEX < $TIMES_LENGTH ; INDEX=INDEX+1)) # First element of array has index 0
  1505.     do
  1506.         SEND_TIME=${USER_CUSTOM_TIMES_TO_SEND_TELEGRAM[$INDEX]} # Get time to send telegram
  1507.         # Check if time has the right format (hh:mm, 0-23 and 0-9)
  1508.         if [[ "$SEND_TIME" =~ $CHECK_TIME_REGEX ]]; then
  1509. #       echo "Time OK : "$SEND_TIME
  1510.  
  1511.         # Check if is time to send telegram according to user customization
  1512.         time_of_day_to_send
  1513.  
  1514.         if [[ $SEND_USER_CUSTOM_TELEGRAM -eq 1 ]]; then
  1515.             break
  1516.         fi
  1517.  
  1518.         else
  1519. #       echo "Time wrong : "$SEND_TIME
  1520.         WRONG_TIME_FORMAT=$SEND_TIME" "$WRONG_TIME_FORMAT
  1521.         fi
  1522.     done
  1523.  
  1524.     # If user custom time format is wrong var "WRONG_TIME_FORMAT" is NOT empty
  1525.     if [[ ! -z "$WRONG_TIME_FORMAT" ]]; then
  1526.         # Remove last char which is a space
  1527.         WRONG_TIME_FORMAT=$(echo $WRONG_TIME_FORMAT | sed 's/ $//')
  1528.         # Insert warning in file as the first line
  1529.         sed -i "2s/^/WARNING: Bad user custom time format in var \"USER_CUSTOM_TIMES_TO_SEND_TELEGRAM\"\nBad times: \"$WRONG_TIME_FORMAT\"\n/" $KK003_WORKBENCH/files/system_data.txt
  1530.  
  1531.         sed -i "2s/^/WARNING: Bad user custom time format in var \"USER_CUSTOM_TIMES_TO_SEND_TELEGRAM\"\nBad times: \"$WRONG_TIME_FORMAT\"\n/" $KK003_WORKBENCH/files/system_data_log_file.txt
  1532.  
  1533.         W_BAD_USER_SEND_TIME_FORMAT=1
  1534.  
  1535.         # Write the warning count (+1) to warning variable
  1536.         modify_warnings_file
  1537.  
  1538.  
  1539.     else        # if there is not warning then reset the value to "0"
  1540.  
  1541.             TOTAL_WARNINGS=0
  1542.             sed -i "/$ELEMENT/ c$ELEMENT${TOTAL_WARNINGS}" $WARNINGS_FILE # I write the new value to file
  1543.  
  1544.     fi
  1545.  
  1546.     fi
  1547.  
  1548. fi
  1549. }
  1550.  
  1551.  
  1552. function check_telegram_send_interval ()
  1553. {
  1554. ELEMENT="W_INTERVAL_SEND_TELEGRAM_CHANGED="
  1555.  
  1556. # Check if "LAST_SEND_TELEGRAM_INTERVAL" exist in data file
  1557. grep -q "LAST_SEND_TELEGRAM_INTERVAL=" $DATA_FILE
  1558. CODE_GREP=$?
  1559. LAST_SEND_TELEGRAM_INTERVAL=$(cat $DATA_FILE | grep "LAST_SEND_TELEGRAM_INTERVAL=" | cut -d"=" -f2) # Get value
  1560.  
  1561. if [[ $CODE_GREP -ne 0 || -z "$LAST_SEND_TELEGRAM_INTERVAL" ]]; then            # If string no found or no value then
  1562.  
  1563.     sed -i "/LAST_SEND_TELEGRAM_INTERVAL=/d" $DATA_FILE                 # Removes ALL lines (just in the rare case of duplicated lines) lines containing the string....
  1564.     echo "LAST_SEND_TELEGRAM_INTERVAL=$TELEGRAM_TIMEOUT_IN_MINUTES" >> $DATA_FILE   # and write interval from 1bash
  1565.    
  1566.     TOTAL_WARNINGS=0
  1567.     sed -i "/$ELEMENT/ c$ELEMENT${TOTAL_WARNINGS}" $WARNINGS_FILE           # I write the new value to file
  1568.     return
  1569. fi
  1570.  
  1571. if [[ "$LAST_SEND_TELEGRAM_INTERVAL" != "" && "$LAST_SEND_TELEGRAM_INTERVAL" != "$TELEGRAM_TIMEOUT_IN_MINUTES" ]]; then     # In this case interval has changed
  1572.     sed -i "/LAST_SEND_TELEGRAM_INTERVAL=/ cLAST_SEND_TELEGRAM_INTERVAL=${TELEGRAM_TIMEOUT_IN_MINUTES}" $DATA_FILE      # Update interval to send telegram to data file
  1573.  
  1574.     sed -i "2s/^/WARNING: Interval in minutes to send telegram has changed from $LAST_SEND_TELEGRAM_INTERVAL to $TELEGRAM_TIMEOUT_IN_MINUTES\n/" $KK003_WORKBENCH/files/system_data.txt
  1575.  
  1576.     sed -i "2s/^/WARNING: Interval in minutes to send telegram has changed from $LAST_SEND_TELEGRAM_INTERVAL to $TELEGRAM_TIMEOUT_IN_MINUTES\n/" $KK003_WORKBENCH/files/system_data_log_file.txt
  1577.  
  1578.     W_INTERVAL_SEND_TELEGRAM_CHANGED=1
  1579.  
  1580.     # Write the warning count (+1) to warning variable and set TOTAL_WARNINGS=1
  1581.     modify_warnings_file
  1582.  
  1583. else # if there is not warning then reset the value to "0"
  1584.  
  1585.     TOTAL_WARNINGS=0
  1586.     sed -i "/$ELEMENT/ c$ELEMENT${TOTAL_WARNINGS}" $WARNINGS_FILE # I write the new value to file
  1587. fi
  1588.  
  1589. }
  1590.  
  1591.  
  1592. function send_telegram ()
  1593. {
  1594. # I send the telegram/s here. I prefer to send the main one first ( got api key and user id form 1bash )
  1595. # and then deal with the addictional ones if there's any
  1596. #
  1597. # Send the mail telegram
  1598. if [[ $SEND_WARNING -eq 1 || $USE_NVOC_DEFAULT_TIMES_TELEGRAM -eq 1 || $SEND_USER_CUSTOM_TELEGRAM -eq 1 ]]; then
  1599.  
  1600.     curl -s -X POST --output /dev/null https://api.telegram.org/bot${APIKEY}/sendMessage -d "text=${MSG_TELEGRAM}" -d chat_id=${CHATID}
  1601.     CODE_CURL=$?
  1602.  
  1603.     # Check if the mgs went out ok
  1604.     if [[ $CODE_CURL -eq 0 ]]; then
  1605.     echo "Msg for main telegram account with:"
  1606.     echo "Api Key      : $APIKEY"
  1607.     echo "Chat/User Id : $CHATID"
  1608.     echo "Sent ok!!!!"
  1609.     else
  1610.     echo "WARNING: Msg for main telegram account with:"
  1611.     echo "Api Key      : $APIKEY"
  1612.     echo "Chat/User Id : $CHATID"
  1613.     echo "probably has not reached its destination"
  1614.     echo "Got a non zero exit code from curl : $CODE_CURL"
  1615.     fi
  1616.  
  1617. sleep 1
  1618.  
  1619. # Send the rest of telegrams if any on array
  1620. # First must check that elements in the array are a even number and is not empty
  1621. TELEGRAM_ADDITIONAL_RECIPIENTS_LENGTH=${#TELEGRAM_ADDITIONAL_RECIPIENTS[@]}                     # Get number of elements
  1622.  
  1623.     if [[ $TELEGRAM_ADDITIONAL_RECIPIENTS_LENGTH -ne 0 ]]; then                         # Is not empty
  1624.  
  1625.     if [[ $((TELEGRAM_ADDITIONAL_RECIPIENTS_LENGTH%2)) -eq 0 ]]; then                       # Number of elements is even?. If so I assume api key and user id / chat id are well matched in array
  1626.  
  1627.             for ((INDEX_EVEN=0; INDEX_EVEN < $TELEGRAM_ADDITIONAL_RECIPIENTS_LENGTH ; INDEX_EVEN=INDEX_EVEN+2)) # First element of array have index 0
  1628.             do                                                                                      # Even elements are the gpu names, odd elements are alias
  1629.  
  1630.             APIKEY=${TELEGRAM_ADDITIONAL_RECIPIENTS[$INDEX_EVEN]}                                       # Get the name of the gpu in array
  1631.             INDEX_ODD=$(($INDEX_EVEN + 1))                                                      # +1 to get the alias index
  1632.             CHATID=${TELEGRAM_ADDITIONAL_RECIPIENTS[$INDEX_ODD]}                                        # Get the alias of the gpu in array
  1633.  
  1634.         # Send the telegram
  1635.         curl -s -X POST --output /dev/null https://api.telegram.org/bot${APIKEY}/sendMessage -d "text=${MSG_TELEGRAM}" -d chat_id=${CHATID}
  1636.         CODE_CURL=$?
  1637.  
  1638.         # Check if the mgs went out ok
  1639.         if [[ $CODE_CURL -eq 0 ]]; then
  1640.             echo "Msg for additional telegram account with:"
  1641.             echo "Api Key      : $APIKEY"
  1642.             echo "Chat/User Id : $CHATID"
  1643.             echo "sent ok!!!!"
  1644.         else
  1645.             echo "WARNING: Msg for additional telegram account with:"
  1646.             echo "Api Key      : $APIKEY"
  1647.             echo "Chat/User Id : $CHATID"
  1648.             echo "probably has not reached its destination"
  1649.             echo "Got a non zero exit code from curl : $CODE_CURL"
  1650.         fi
  1651.         sleep 1
  1652.         done
  1653.  
  1654.     # I get this way out if number of elements in array are odd and I don't send any additional telegram
  1655.     else
  1656.             echo "Number of elements in variable TELEGRAM_ADDITIONAL_RECIPIENTS are odd and I don't send any additional telegram."
  1657.             echo "Check variable TELEGRAM_ADDITIONAL_RECIPIENTS if you want to send additional telegrams"
  1658.     fi
  1659.  
  1660. # I get this way out if array in variable MODELS is empty
  1661.     else
  1662.     echo "Variable TELEGRAM_ADDITIONAL_RECIPIENTS is empty and I don't send any additional telegram."
  1663.     echo "Check variable TELEGRAM_ADDITIONAL_RECIPIENTS if you want to send additional telegrams"
  1664.     fi
  1665. fi
  1666. }
  1667.  
  1668. function source_1bash ()
  1669. {
  1670. # Get some vars from 1bash if exists
  1671. ONE_BASH=0  # 0=Assume 1bash exists and its in place, 1=1bash is missing
  1672. if [[ -f ~/1bash ]]; then
  1673.     source ~/1bash
  1674.  
  1675.     # If TELEGRAM_TIMEOUT_IN_MINUTES in 1bash is not seted up I must leave the script
  1676.     TELEGRAM_TIMEOUT_IN_MINUTES_EMPTY=0
  1677.     if [[ -z $TELEGRAM_TIMEOUT_IN_MINUTES ]]; then
  1678.     TELEGRAM_TIMEOUT_IN_MINUTES_EMPTY=1
  1679.     salida
  1680.     else
  1681.     ONE_BASH_TELEGRAM_INTERVAL_IN_SECONDS=$(($TELEGRAM_TIMEOUT_IN_MINUTES * 60))
  1682.     fi
  1683.  
  1684.     # If TELEGRAM_CHATID in 1bash is not seted up I must leave the script
  1685.     TELEGRAM_CHATID_EMPTY=0
  1686.     if [[ -z $TELEGRAM_CHATID ]]; then
  1687.     TELEGRAM_CHATID_EMPTY=1
  1688.     salida
  1689.     else
  1690.     CHATID=$TELEGRAM_CHATID
  1691.     fi
  1692.  
  1693.     # If TELEGRAM_APIKEY in 1bash is not seted up I must leave the script
  1694.     TELEGRAM_APIKEY_EMPTY=0
  1695.     if [[ -z $TELEGRAM_APIKEY ]]; then
  1696.     TELEGRAM_APIKEY_EMPTY=1
  1697.     salida
  1698.     else
  1699.     APIKEY=$TELEGRAM_APIKEY
  1700.     fi
  1701.  
  1702. else # 1bash not found
  1703.  
  1704.     ONE_BASH=1 # 1bash is missing
  1705.     salida
  1706. fi
  1707.  
  1708.  
  1709. }
  1710.  
  1711. function nvoc_script_requested_telegram ()
  1712. {
  1713. # Check if some Nvoc system script want to send a telegram and give priority to it
  1714.  
  1715. #
  1716. # Array with list of names for system scripts that may want to send a telegram
  1717. #
  1718. NVOC_SYSTEM_SCRIPTS_MAY_REQUEST_TELEGRAM=(
  1719.                         "IAmNotAJeep_and_Maxximus007_WATCHDOG"
  1720.                         )
  1721.  
  1722. # Get name of parent process that has requested a telegram
  1723. PARENT_PROCESS_NAME=$(cat /proc/$PPID/cmdline)
  1724. echo "Parent process name  : " $PARENT_PROCESS_NAME
  1725.  
  1726. for SCRIPT_NAME in "${NVOC_SYSTEM_SCRIPTS_MAY_REQUEST_TELEGRAM[@]}"
  1727. do
  1728.     # Check for script name element in array
  1729.     echo $PARENT_PROCESS_NAME | grep -oq "$SCRIPT_NAME"
  1730.     if [[ $? -eq 0  ]]; then                                        # Script name found
  1731.     echo "Parent script name  : " $SCRIPT_NAME
  1732.     NAME_SYSTEM_SCRIPT_REQUESTED_TELEGRAM=$SCRIPT_NAME
  1733.     SEND_WARNING=1                                              # Send telegram warning triged by system script
  1734.     SYSTEM_SCRIPT_REQUESTED_TELEGRAM=1
  1735.     fi
  1736.  
  1737. done
  1738.  
  1739. }
  1740.  
  1741. function valida_ip ()
  1742. {
  1743. local  ip=$1
  1744. ESTADO_TEST_IP=1
  1745.  
  1746. if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  1747.     OIFS=$IFS
  1748.     IFS='.'
  1749.     ip=($ip)
  1750.     IFS=$OIFS
  1751.     [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
  1752.         && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
  1753.     ESTADO_TEST_IP=$?
  1754. fi
  1755. return $ESTADO_TEST_IP
  1756. }
  1757.  
  1758.  
  1759. function salida ()
  1760. {
  1761. END_TIME=`date "+%Y-%m-%d %H:%M:%S"`
  1762.  
  1763. echo
  1764.  
  1765. if [[ $ONE_BASH -eq 1 || $TELEGRAM_TIMEOUT_IN_MINUTES_EMPTY -eq 1 || $TELEGRAM_CHATID_EMPTY -eq 1 || $TELEGRAM_APIKEY_EMPTY -eq 1 || $MAIN_TELEGRAM_TIME_COUNTER_MISSING -eq 1 ]]; then
  1766.  
  1767.     if [[ $ONE_BASH -eq 1 ]]; then # Ensure in 1bash exists
  1768.     echo "WARNING: 1bash file is missing (/home/m1/1bash)"
  1769.     fi
  1770.  
  1771.     if [[ $TELEGRAM_TIMEOUT_IN_MINUTES_EMPTY -eq 1 ]]; then # Ensure TELEGRAM_TIMEOUT_IN_MINUTES in 1bash has a value
  1772.     echo "WARNING: variable TELEGRAM_TIMEOUT_IN_MINUTES in 1bash is empty. Configure the variable before send a telegram (/home/m1/1bash)"
  1773.     fi
  1774.  
  1775.     if [[ $TELEGRAM_CHATID_EMPTY -eq 1 ]]; then # Ensure TELEGRAM_CHATID in 1bash has a value
  1776.     echo "WARNING: variable TELEGRAM_CHATID in 1bash is empty. Configure the variable before send a telegram (/home/m1/1bash)"
  1777.     fi
  1778.  
  1779.     if [[ $TELEGRAM_APIKEY_EMPTY -eq 1 ]]; then # Ensure TELEGRAM_APIKEY_EMPTY in 1bash has a value
  1780.     echo "WARNING: variable TELEGRAM_APIKEY_EMPTY in 1bash is empty. Configure the variable before send a telegram (/home/m1/1bash)"
  1781.     fi    
  1782.  
  1783.     if [[ $MAIN_TELEGRAM_TIME_COUNTER_MISSING -eq 1 ]]; then # Ensure main time counter file is
  1784.     echo "WARNING: main time counter file is missing: $MAIN_TELEGRAM_TIME_COUNTER_1 and/or $MAIN_TELEGRAM_TIME_COUNTER_2"
  1785.     fi
  1786.  
  1787.     # I get out this way as I am missing some parameter to send the telegram, so I write the problem to the log file and exit
  1788.     bad_exit
  1789. fi
  1790.  
  1791. if [[ $SEND_WARNING -eq 1 || $USE_NVOC_DEFAULT_TIMES_TELEGRAM -eq 1 || $SEND_USER_CUSTOM_TELEGRAM -eq 1 ]]; then
  1792.  
  1793.     echo "Telegram sent: YES"
  1794.     echo "Message follows :"
  1795. else
  1796.     echo "Telegram sent: NO"
  1797.     echo "There was no warnings or telegram scheduled to send."
  1798.     echo "Just saving report to log file :"
  1799.     echo
  1800. fi
  1801.  
  1802. # Dump the data collected to the log file (just have to do a echo becouse I am using the "(" and ")" around the code I'm interested in)
  1803. echo "$MSG_LOG_FILE"
  1804. echo
  1805. echo "End time: " $END_TIME
  1806.  
  1807. echo
  1808. echo "**************************"
  1809. echo
  1810. exit
  1811. }
  1812.  
  1813. function bad_exit ()
  1814. {
  1815. echo "Telegram sent: NO"
  1816. echo "Saved report to log file :"
  1817. echo
  1818.  
  1819. # Dump the data collected to the log file (just have to do a echo becouse I am using the "(" and ")" around the code I'm interested in).
  1820. echo "$MSG_LOG_FILE"
  1821. echo
  1822. echo "End time: " $END_TIME
  1823.  
  1824. echo
  1825. echo "**************************"
  1826. echo
  1827. exit
  1828.  
  1829. }
  1830.  
  1831.  
  1832. #
  1833. ### Main body code starts here ###
  1834. #
  1835.  
  1836. echo "Start time: " $START_TIME
  1837.  
  1838. # By defaul don't send telegram triged by warnings
  1839. SEND_WARNING=0 # set to "0"
  1840.  
  1841. # By defaul I say no known miner is running
  1842. KNOWN_MINER_RUNNING=0 # "0" not a known miner running, "1" found a known miner running
  1843.  
  1844. # By defaul I say non system script has requested a telegram
  1845. SYSTEM_SCRIPT_REQUESTED_TELEGRAM=0
  1846.  
  1847. # Check if some Nvoc system script want to send a telegram
  1848.     nvoc_script_requested_telegram
  1849.  
  1850. # Nvoc sends a telegram as soon as it starts running and that causes
  1851. # some values not to be available.
  1852. # Same applies in case Xorg has been restarted so
  1853. # to avoid this I wait three minutes before send the telegram msg
  1854. # when I found out any of these two cases
  1855.  
  1856. TIME_TO_WAIT_IN_SEGS=180                            # Don't send a telegram at less the system and Xorg is up for more than 180 segs
  1857.  
  1858. SYSTEM_RESTARTED=0                              # 1=system just restarted, 0=system has been operating for more than 3 minutes
  1859. SYSTEM_TIME_UP_IN_SEGS=$(cat /proc/uptime | cut -d"." -f1)          # Get the system's time up in seconds
  1860. # Check system up time
  1861. if [[ $SYSTEM_TIME_UP_IN_SEGS -le $TIME_TO_WAIT_IN_SEGS ]]; then
  1862.     SYSTEM_RESTARTED=1                              # 1=system just restarted, 0=system has been operating for more than 3 minutes
  1863.    
  1864.     WAITING_SEGS=$(($TIME_TO_WAIT_IN_SEGS - $SYSTEM_TIME_UP_IN_SEGS))
  1865.     echo "System just started. Waiting $WAITING_SEGS seconds before data collect"
  1866.     sleep $WAITING_SEGS
  1867.  
  1868. fi
  1869.  
  1870. XORG_RESTARTED=0                                # 1=Xorg just restarted, 0=Xorg has been operating for more than 3 minutes
  1871. ps aux | grep -v grep | grep -q "Xorg"                                          # Check if procces exists
  1872. if [[ $? -eq 0 ]]; then
  1873.     XORG_TIME_UP_IN_SEGS=$(ps -o etimes= -p $(pgrep -f Xorg) | sed 's/ //g')    # Get the Xorg's time up in seconds
  1874.  
  1875.     # Check Xorg up time
  1876.     if [[ $XORG_TIME_UP_IN_SEGS -le $TIME_TO_WAIT_IN_SEGS ]]; then
  1877.     XORG_RESTARTED=1                            # 1=Xorg just restarted, 0=Xorg has been operating for more than 3 minutes
  1878.  
  1879.     WAITING_SEGS=$(($TIME_TO_WAIT_IN_SEGS - $XORG_TIME_UP_IN_SEGS))
  1880.     echo "Xorg just started. Waiting $WAITING_SEGS seconds before data collect"
  1881.     sleep $WAITING_SEGS
  1882.     fi
  1883. else
  1884.  
  1885.     echo "Xorg process seems to be missing"
  1886. fi
  1887.  
  1888. # Check source 1bash
  1889.     source_1bash
  1890.  
  1891. # I allways check the state of the warnings file
  1892.     regenerate_warnings_file
  1893.  
  1894. #
  1895. # Collect some info from system
  1896. #
  1897.  
  1898. # Number of gpus nvidia-smi reports
  1899. GPU_COUNT=$(nvidia-smi --query-gpu=count --format=csv,noheader,nounits | tail -1)
  1900.  
  1901. # Get system info
  1902.     system_info
  1903.  
  1904. # I make sure the current interval for running this script is equal to the value of var "TELEGRAM_TIMEOUT_IN_MINUTES" in 1bash
  1905. # For it I save the value of var TELEGRAM_TIMEOUT_IN_MINUTES in 1bash on startup and compare it with the current value when running this script
  1906. # If different I send a warning and restart service
  1907.     check_telegram_send_interval
  1908.  
  1909. # Check if is time to send a telegram ordered by user (array USER_CUSTOM_TIMES_TO_SEND_TELEGRAM)
  1910. # Its better leave this call here so I can use system_data.txt to alert the user if there is a bad time format
  1911.     custom_telegram_ordered_by_user
  1912.  
  1913. # Get minimal report or not from nvidia-smi
  1914.  
  1915. if [[ $MODE_NVIDIA_SMI_REPORT -eq 1 || $MODE_NVIDIA_SMI_REPORT -eq 2 ]]; then
  1916.  
  1917.     nvidia_smi_minimal_report
  1918.  
  1919.     # Insert the Title for nvidia-smi report
  1920.     sed -i "1s/^/-- Nvidia-smi Information :\n/" $KK003_WORKBENCH/files/nvidia_smi_values.csv
  1921.     sed -i "1s/^/-- Nvidia-smi Information :\n/" $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  1922.  
  1923. else
  1924.  
  1925.     # ### block for nvidia-smi report code (do not change the order of these lines or you may not get correctly the output of section "nvidia-smi information")
  1926.     #
  1927.     # Get the gpus's info I am interested in
  1928.     nvidia-smi --query-gpu=$NVIDIA_SMI_QUERY_ARGUMENTS --format=csv -f $KK003_WORKBENCH/files/nvidia_smi_values.csv
  1929.  
  1930.     cp $KK003_WORKBENCH/files/nvidia_smi_values.csv $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  1931.  
  1932.     # I organize some things my way
  1933.     if [[ "$USE_function_my_custom_mods_for_nvidia_smi" == "YES" ]]; then
  1934.  
  1935.     my_custom_mods_for_nvidia_smi
  1936.  
  1937.     fi
  1938.  
  1939.     # Insert the Title for nvidia-smi report
  1940.     sed -i "1s/^/-- Nvidia-smi Information :\n/" $KK003_WORKBENCH/files/nvidia_smi_values.csv
  1941.     sed -i "1s/^/-- Nvidia-smi Information :\n/" $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv
  1942.     #
  1943.     # ### End of block for nvidia-smi report code
  1944. fi
  1945.  
  1946. # Collect Claymore statics if configured so
  1947. claymore_statics
  1948.  
  1949. # Collect the Genoil statics if configured so
  1950. genoil_statics
  1951.  
  1952. # Collect the ethminer statics if configured so
  1953. ethminer_statics
  1954.  
  1955. # Find out if there is any Known miner running
  1956. if [[ $KNOWN_MINER_RUNNING -eq 0 ]]; then   # 0= there is NOT any Known miner running
  1957.     echo "WARNING: There is not a Known miner running from where to get mining statics!!!"
  1958. fi
  1959.  
  1960. # Set system info+nvidia-smi+mining info together
  1961. if [[ "$USE_CLAYMORE_STATICS" == "YES" && "$CLAYMORE_IS_RUNNING" == "YES" || "$USE_GENOIL_STATICS" == "YES" && "$GENOIL_IS_RUNNING" == "YES" || "$USE_ETHMINER_STATICS" == "YES" && "$ETHMINER_IS_RUNNING" == "YES" ]]; then
  1962.     cat $KK003_WORKBENCH/files/system_data.txt $KK003_WORKBENCH/files/nvidia_smi_values.csv $KK003_WORKBENCH/files/miner_statics.txt > $KK003_WORKBENCH/files/report.txt
  1963.  
  1964.     cat $KK003_WORKBENCH/files/system_data_log_file.txt $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv $KK003_WORKBENCH/files/miner_statics_log_file.txt > $KK003_WORKBENCH/files/report_log_file.txt
  1965. else
  1966.     cat $KK003_WORKBENCH/files/system_data.txt $KK003_WORKBENCH/files/nvidia_smi_values.csv > $KK003_WORKBENCH/files/report.txt
  1967.  
  1968.     cat $KK003_WORKBENCH/files/system_data_log_file.txt $KK003_WORKBENCH/files/nvidia_smi_values_log_file.csv > $KK003_WORKBENCH/files/report_log_file.txt
  1969. fi
  1970.  
  1971. # Before send the telegram check if length >= 4096 UTF8 characters
  1972. # If so I just send the warning, no the full data
  1973. LENGTH_MSG=$(cat $KK003_WORKBENCH/files/report.txt | wc -m)
  1974.  
  1975. if [[ $LENGTH_MSG -ge $TELEGRAM_CURRENT_CHARS_LIMT ]]; then
  1976.  
  1977.     # I set SEND_WARNING=1 so I allways send a warning telegram when telegram is too long and telegram server would reject it
  1978.     # Note I do not treat this warning like others; don't write it to file, don't count it, etc
  1979.     SEND_WARNING=1
  1980.  
  1981.     TOO_MUCH_CHARS=$(($LENGTH_MSG - $TELEGRAM_CURRENT_CHARS_LIMT))
  1982.  
  1983.     # Delete all lines in report file from the system information headers (included) to bottom
  1984.     # Note that I keep untouched the log report file ($KK003_WORKBENCH/files/report_log_file.txt) as there is not charaters limit there
  1985.     sed -i "/$TITLE_SYSTEM_INFORMATION/Q" $KK003_WORKBENCH/files/report.txt
  1986.  
  1987.     # Insert a warning saying I don't send the msg becouse telegram server will reject it
  1988.     sed -i "2s/^/WARNING: Telegram is $LENGTH_MSG charaters long, limit is $TELEGRAM_CURRENT_CHARS_LIMT.\nThere is $TOO_MUCH_CHARS characters left. Telegram server will reject this message so you must customize your preferences to decrease the length of the message.\n/" $KK003_WORKBENCH/files/report.txt
  1989.  
  1990.     sed -i "2s/^/WARNING: Telegram is $LENGTH_MSG charaters long, limit is $TELEGRAM_CURRENT_CHARS_LIMT.\nThere is $TOO_MUCH_CHARS characters left. Telegram server will reject this message so you must customize your preferences to decrease the length of the message.\n/" $KK003_WORKBENCH/files/report_log_file.txt
  1991. else
  1992.  
  1993.     # Always show number of characters in log file
  1994.     echo "Number of characters in telegram Msg :" $LENGTH_MSG
  1995. fi
  1996.  
  1997. CONTENT_TELEGRAM=$(cat $KK003_WORKBENCH/files/report.txt)
  1998.  
  1999. CONTENT_LOG_FILE=$(cat $KK003_WORKBENCH/files/report_log_file.txt)
  2000.  
  2001. MSG_TELEGRAM="$CONTENT_TELEGRAM"
  2002.  
  2003. MSG_LOG_FILE="$CONTENT_LOG_FILE"
  2004.  
  2005. # Send telegram/s
  2006. send_telegram
  2007.  
  2008. # If telegram send interval changed I restart the "service" in order to use the new interval and get the hole telegram system synchronized
  2009. # If kk003_TELEGRAM_ALERTS exits I should be on Nvoc 19 stable
  2010. # If BaliMiner_TELEGRAM_ALERTS exists but kk003_TELEGRAM_ALERTS does not I should be on Nvoc 19+ or highest
  2011.  
  2012. MAIN_TELEGRAM_TIME_COUNTER_1=~/kk003_TELEGRAM_ALERTS
  2013. MAIN_TELEGRAM_TIME_COUNTER_2=~/BaliMiner_TELEGRAM_ALERTS
  2014. MAIN_TELEGRAM_TIME_COUNTER_3=~/7telegram
  2015.  
  2016. if [[ -f "$MAIN_TELEGRAM_TIME_COUNTER_1" ]]; then                   # In this case running Nvoc 19 stable
  2017.     MAIN_TELEGRAM_TIME_COUNTER=$MAIN_TELEGRAM_TIME_COUNTER_1
  2018. elif [[ -f "$MAIN_TELEGRAM_TIME_COUNTER_2" ]]; then                 # In this case running Nvoc 19+ or highest
  2019.     MAIN_TELEGRAM_TIME_COUNTER=$MAIN_TELEGRAM_TIME_COUNTER_2
  2020. elif [[ -f "$MAIN_TELEGRAM_TIME_COUNTER_3" ]]; then                 # In this case running Nvoc 19.2 or highest
  2021.     MAIN_TELEGRAM_TIME_COUNTER=$MAIN_TELEGRAM_TIME_COUNTER_3
  2022. else
  2023.     MAIN_TELEGRAM_TIME_COUNTER_MISSING=1
  2024.     salida # exit
  2025. fi
  2026.  
  2027. if [[ $SYNC_TELEGRAM_SYSTEM -eq 1 ]]; then                      # SYNC_TELEGRAM_SYSTEM=1 then Sync telegram system
  2028.  
  2029.     if [[ $W_INTERVAL_SEND_TELEGRAM_CHANGED -eq 1 ]]; then              # If eq 1 the user has changed the interval in 1bash
  2030.         echo "Current send telegram interval and interval in 1bash file are not equal"
  2031.         echo "Killing and restarting $MAIN_TELEGRAM_TIME_COUNTER"
  2032.  
  2033.         pkill -f $MAIN_TELEGRAM_TIME_COUNTER
  2034.         if [[ $? -eq 0 ]]; then
  2035.         echo "Ok, Telegram system successfully stopped"
  2036.         bash $MAIN_TELEGRAM_TIME_COUNTER &
  2037.         if [[ $? -eq 0 ]]; then
  2038.             echo "Ok, Telegram system successfully started"
  2039.         else
  2040.             echo "ERROR, Telegram system failed to restart"
  2041.         fi
  2042.  
  2043.     else # Try again
  2044.         sleep 2
  2045.  
  2046.         pkill -f $MAIN_TELEGRAM_TIME_COUNTER
  2047.         if [[ $? -eq 0 ]]; then
  2048.             echo "Ok, Telegram system successfully stopped (on second try)"
  2049.             bash $MAIN_TELEGRAM_TIME_COUNTER &
  2050.         if [[ $? -eq 0 ]]; then
  2051.             echo "Ok, Telegram system successfully started"
  2052.         else
  2053.             echo "ERROR, Telegram system failed to restart"
  2054.         fi
  2055.         fi
  2056.  
  2057.     fi
  2058.     fi
  2059. fi
  2060.  
  2061. salida # exit
  2062.  
  2063. ) >> $KK003_LOG_FILE ##### DON'T REMOVE THIS CLOSED PARENTHESIS OR WE'LL MISS THE LOG FILE
Advertisement
Add Comment
Please, Sign In to add comment