Advertisement
kk003

kk003_telegram v1.5

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