File Share - Share your files :: Sandal Computers 

Network Connectivity Monitor

Shared on 2022-10-21 22:44:50 from 2.217.78.38

Logs, charts and alerts from pining a host, with router check.

  1. #!/bin/bash
  2. #
  3. # Monitor internet/host availablility
  4. #
  5. # (c) Stin, 2021-22 - Modifed for Mark's RPi, 10/2022
  6. #
  7. ############################################
  8. ### Configurable parameters below:
  9. # Ping host <test_ip>...
  10. test_ip=192.168.0.10
  11. # Ping router <router_ip>
  12. router_ip=
  13. # ...<p_count> times...
  14. p_count=5
  15. # ...look for <f_count> failures...
  16. f_count=4
  17. # ...wait <p_tmout> seconds for a reponse...
  18. p_tmout=4
  19. # ...wait <p_delay> seconds between pings.
  20. p_delay=5
  21. ############################################
  22. ### Outputs:
  23. # Folder to save log files into:
  24. logdir="Allotment Connection"
  25. mkdir -p "$logdir"
  26. # 2>/dev/null 1>&2
  27. # Save/append state changes to a log file:
  28. logfile="$logdir/Allotment_Connection"
  29. # Or a timestamped log file:
  30. #logfile="./Internet_Connection_$(date +%Y%m%d-%H%M%S)"
  31. # Optionally, save/append ping data to a csv file (if set, comment to disable):
  32. csvfile="$logdir/Allotment_Connection"
  33. # Syslog output:
  34. # 0=No updates;
  35. # 1=Connectivity changes;
  36. # 2=Each ping check;
  37. logger=1
  38. ############################################
  39. ### Do not touch ;)
  40. # Initial Internet state
  41. inet_state=2
  42. # Initial `last down` time:
  43. down_time=--
  44. # Format date and time for reporting:
  45. time_format='%H:%M.%S'
  46. date_format='%d/%m/%Y'
  47. # Timestamp for log_stamp()
  48. log_timestamp=$(date +%Y%m%d)
  49. ############################################
  50. ### Let's-a go!
  51. echo -e "\e[30;43m"; clear
  52. log_stamp () {
  53. mv "$logfile.log" "$logfile.$log_timestamp.log"
  54. mv "$csvfile.csv" "$csvfile.$log_timestamp.csv"
  55. create_output_files
  56. }
  57. create_output_files () {
  58. # Start date for log rotation:
  59. ldate=$(date +$date_format)
  60. if [ $logger -ge 1 ]; then logger "$logdir alarm initiated. Parameters: P_IP=$test_ip P_T=${p_tmout} P_D=${p_delay} R_IP=$router_ip F/P=${f_count}/${p_count}"; fi
  61. # Create the log file with header if it doesn't exist already...
  62. if [ ! -f "$logfile.log" ]; then
  63. echo Log created at $ldate > "$logfile.log"
  64. echo Pinging $test_ip $p_count time\(s\). Looking for $f_count failures... | tee "$logfile.log"
  65. #nslookup $test_ip
  66. echo Timeout: ${p_tmout}s - Interval: ${p_delay}s - Fail threshold: ${f_count}/${p_count} | tee -a "$logfile.log"
  67. else
  68. echo Appending to main log file : $logfile.log
  69. fi
  70. if [ "$csvfile.csv" != "" ]; then
  71. if [ ! -f "$csvfile.csv" ]; then
  72. echo Creating CSV output file : $csvfile.csv
  73. echo \"Date\",\"Time\",\"State\",\"Sent\",\"Replies\",\"Total Time \(ms\)\",\"Avg. Ping \(ms\)\",\"Last change\"> "$csvfile.csv"
  74. else
  75. echo Appending CSV output to file : $csvfile.csv
  76. fi
  77. fi
  78. }
  79. create_output_files
  80. while true; do
  81. if [ ! -z $router_ip ] ; then
  82. router_r=$(ping -W 10 -c $p_count $router_ip | grep "packet loss" || echo $p_count packets transmitted, 0 received, 100% packet loss, time 0ms)
  83. else
  84. router_r="0 packets transmitted, 0 received, 0% packet loss, time 0ms"
  85. fi
  86. # Clear variables before use:
  87. fails=
  88. replies=
  89. sent=
  90. # Parse ping command output:
  91. # Output: 3 packets transmitted, 0 received, 100% packet loss, time 2038ms
  92. sent=$(echo $router_r | cut -d "," -f 1 | cut -d " " -f 1)
  93. replies=$(echo $router_r | cut -d "," -f 2 | cut -d " " -f 1-2)
  94. tottime=$(echo $router_r | cut -d "," -f 4 | cut -d " " -f 3 | cut -d "m" -f 1)
  95. fails=$(($sent - $replies))
  96. #echo $timestamp :: Router $router_ip check result: $replies/$sent replies received. $fails requests failed to return.
  97. if [ $fails -ge $f_count ]; then
  98. # Display RED window:
  99. echo -ne "\e[30;41m"
  100. clear
  101. tail "$logfile.log"
  102. # The router is down, or we can't get to the router:
  103. echo $timestamp :: Router $router_ip is unavailable. $fails\/$sent fails. | tee -a "$logfile.log"
  104. if [ $logger -ge 1 ]; then logger "$timestamp :: Router $router_ip is unavailable. $fails/$sent fails."; fi
  105. # Output to csv file, if set:
  106. if [ "$csvfile.csv" != "" ]; then
  107. echo $event_date,$event_time,\"Router $router_ip unavailable\",$sent,$(($replies)),0,0,>> "$csvfile.csv"
  108. fi
  109. else
  110. # The router is fine, let's check the Internet:
  111. # Ping the host <n> times:
  112. ping_r=$(ping -W $p_tmout -c $p_count $test_ip | grep "packet loss" || echo $p_count packets transmitted, 0 received, 100% packet loss, time 0ms)
  113. logger $(echo $ping_r | xargs)
  114. # Clear variables before use:
  115. fails=
  116. replies=
  117. sent=
  118. # Parse ping command output:
  119. # Output: 3 packets transmitted, 0 received, 100% packet loss, time 2038ms
  120. sent=$(echo $ping_r | cut -d "," -f 1 | cut -d " " -f 1)
  121. replies=$(echo $ping_r | cut -d "," -f 2 | cut -d " " -f 1-2)
  122. tottime=$(echo $ping_r | cut -d "," -f 4 | cut -d " " -f 3 | cut -d "m" -f 1)
  123. fails=$(($sent - $replies))
  124. # Create time string for this pass:
  125. event_date=$(date +$date_format)
  126. event_time=$(date +$time_format)
  127. timestamp="$event_date $event_time"
  128. # If event date is NOT today, rotate the log files and recreate them:
  129. if [ "$ldate" != "$event_date" ]; then
  130. echo Log rotation:
  131. log_stamp
  132. log_datestamp=$(date +%Y%m%d)
  133. fi
  134. # Detect failure and change in state:
  135. if [ $fails -ge $f_count ]; then
  136. # Just started. Connection is DOWN.
  137. if [ $inet_state -eq 2 ]; then
  138. # Display RED window:
  139. echo -ne "\e[30;41m"
  140. clear
  141. tail "$logfile.log"
  142. echo $timestamp :: $logdir is down. $fails\/$sent fails. | tee -a "$logfile.log"
  143. if [ $logger -ge 1 ]; then logger "$timestamp :: $logdir is down. $fails/$sent fails."; fi
  144. down_time=$timestamp
  145. fi
  146. # Connection was UP. It is now DOWN. Record time.
  147. if [ $inet_state -eq 1 ]; then
  148. # Display RED window:
  149. echo -ne "\e[30;41m"
  150. clear
  151. tail "$logfile.log"
  152. echo $timestamp :: $logdir is now down. $fails\/$sent fails. | tee -a "$logfile.log"
  153. if [ $logger -ge 1 ]; then logger "$timestamp :: $logdir is now down. $fails/$sent fails."; fi
  154. down_time=$timestamp
  155. fi
  156. # Output to csv file, if set:
  157. if [ "$csvfile.csv" != "" ]; then
  158. echo $event_date,$event_time,\"Down\",$sent,$(($replies)),$tottime,$(( ($tottime - (($p_count-1) * 1000)) / ($p_count-1) )),>> "$csvfile.csv"
  159. fi
  160. # Output to Syslog via logger, if set:
  161. if [ $logger -eq 2 ]; then
  162. logger "$logdir status: DOWN -- $(($replies)) of $sent Successful; Ping time: ${tottime}ms; Avg. Ping time: $(( (${tottime} - ((${p_count} - 1) * 1000)) / (${p_count} - 1) ))ms"
  163. fi
  164. inet_state=0
  165. else
  166. # Just started. Connection is UP.
  167. if [ $inet_state -eq 2 ]; then
  168. # Display GREEN window:
  169. echo -ne "\e[30;42m"
  170. clear
  171. tail "$logfile.log"
  172. echo $timestamp :: $logdir is up. $fails\/$sent fails. | tee -a "$logfile.log"
  173. if [ $logger -ge 1 ]; then logger "$timestamp :: $logdir is up. $fails/$sent fails."; fi
  174. fi
  175. # Connection was DOWN. It is now UP. Report time.
  176. if [ $inet_state -eq 0 ]; then
  177. # Display GREEN window:
  178. echo -ne "\e[30;42m"
  179. clear
  180. tail "$logfile.log"
  181. echo $timestamp :: $logdir is now up. $fails\/$sent fails. Down since ${down_time}. | tee -a "$logfile.log"
  182. if [ $logger -ge 1 ]; then logger "$timestamp :: $logdir is now up. $fails/$sent fails. Down since ${down_time}."; fi
  183. fi
  184. # Output to csv file, if set:
  185. if [ "$csvfile.csv" != "" ]; then
  186. # echo $event_date,$event_time,\"Up\",$sent,$(($replies)),$tottime,$down_time>> "$csvfile.csv"
  187. echo $event_date,$event_time,\"Up\",$sent,$(($replies)),$tottime,$(( ($tottime - (($p_count-1) * 1000)) / ($p_count-1) )),$down_time>> "$csvfile.csv"
  188. fi
  189. # Output to Syslog via logger, if set:
  190. if [ $logger -eq 2 ]; then logger "$logdir status: UP -- $(($replies)) of $sent Successful; Ping time: ${tottime}ms; Avg. Ping time: $(( (${tottime} - ((${p_count} - 1) * 1000)) / (${p_count} - 1) ))ms; Down since: $down_time"; fi
  191. inet_state=1
  192. down_time=--
  193. fi
  194. sleep $p_delay
  195. fi
  196. done
  197. ############################################
  198. ### gEOFf

Latest shares…

  1. New update for F, G and H boxes
    1 file by 206.245.204.7 on 2025-06-17 00:21:31
  2. Mum's 70th party invites
    2 files by 192.168.42.138 on 2024-08-22 02:07:16
  3. Mick & the 3 Radicals
    8 files by 2.122.167.219 on 2024-08-06 22:40:38
  4. UnlockTool 2024.07.02.1
    3 files by 192.168.42.138 on 2024-07-14 14:50:47
  5. Windows Tools
    1 file by 192.168.42.138 on 2024-07-05 01:52:42
  6. Update for 'F' and 'G' boxes
    1 file by 92.18.228.86 on 2024-07-04 18:37:15
  7. TV Box Update
    1 file by 2.102.239.118 on 2023-09-25 17:16:08
  8. XigmaNAS Issue
    1 file by 192.168.42.138 on 2023-08-31 01:53:04
  9. leeds.gov.uk - Report loop
    3 files by 51.198.178.127 on 2023-02-18 18:07:11
  10. Horusdy Cable Strippers
    3 files by 51.198.178.127 on 2022-12-30 16:22:41
  1. Network Connectivity MonitorBy 2.217.78.38 on 2022-10-21 22:44:50
  2. Outputting a zero-terminated string, 65c02 asmBy 2.217.78.38 on 2022-10-23 03:08:58
  3. ada0 SMART reportBy 192.168.42.138 on 2025-04-01 15:55:46