|
@@ -0,0 +1,103 @@
|
|
1
|
+#!/bin/sh
|
|
2
|
+HOSTS_FILES="$HOME/.hosts_scanner"
|
|
3
|
+#HOSTS_FILES="/tmp/hosts"
|
|
4
|
+verbose=0
|
|
5
|
+res_file=$(mktemp -t hosts_scanner.XXXXXXXXXX)
|
|
6
|
+pids=""
|
|
7
|
+date_cmd="date --rfc-3339=s"
|
|
8
|
+
|
|
9
|
+lock="${res_file}.lock"
|
|
10
|
+
|
|
11
|
+clean_exit() {
|
|
12
|
+ echo "Ctrl+C pressed, exiting..."
|
|
13
|
+ for pid in $(echo $pids)
|
|
14
|
+ do
|
|
15
|
+ kill $pid
|
|
16
|
+ echo $pid killed
|
|
17
|
+ done
|
|
18
|
+ rmdir ${res_file}.lock 2>/dev/null
|
|
19
|
+ rm -v $res_file
|
|
20
|
+ exit 0
|
|
21
|
+}
|
|
22
|
+
|
|
23
|
+semlock() {
|
|
24
|
+ while [ 1 ]
|
|
25
|
+ do
|
|
26
|
+ mkdir $1 2>/dev/null && break
|
|
27
|
+ done
|
|
28
|
+}
|
|
29
|
+
|
|
30
|
+semrelease() {
|
|
31
|
+ rmdir $1
|
|
32
|
+}
|
|
33
|
+
|
|
34
|
+trap clean_exit 2
|
|
35
|
+
|
|
36
|
+upd_ping() {
|
|
37
|
+ # update ping stat file
|
|
38
|
+ # $1 host
|
|
39
|
+ # $2 file
|
|
40
|
+ # $3 lock
|
|
41
|
+ lock="$3"
|
|
42
|
+ last_up="never"
|
|
43
|
+
|
|
44
|
+ while [ 1 ]
|
|
45
|
+ do
|
|
46
|
+ rep=$(ping -qc 1 $1 2>/dev/null )
|
|
47
|
+ ret=$?
|
|
48
|
+ rep=$(echo $rep | sed -nE 's/.*rtt min\/avg\/max\/mdev = [0-9]+\.[0-9]+\/([0-9]+\.[0-9]+)\/.*$/\1/p')
|
|
49
|
+ rehost=$(echo $h|sed 's/\./\\./g')
|
|
50
|
+
|
|
51
|
+ semlock $lock
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+ if [ $ret -eq 0 ]
|
|
55
|
+ then
|
|
56
|
+ last_up=$($date_cmd)
|
|
57
|
+ #echo "${res}$rep $hstr $last_up" >> $2
|
|
58
|
+ else
|
|
59
|
+ rep="down"
|
|
60
|
+ #echo "${res}down $hstr $last_up" >> $2
|
|
61
|
+ fi
|
|
62
|
+
|
|
63
|
+ sed -i "s/^\\s*$rehost\\s.*$/$(printf "%30s %8s %30s\n" $1 $rep "$last_up")/" "$2"
|
|
64
|
+
|
|
65
|
+ semrelease $lock
|
|
66
|
+
|
|
67
|
+ sleep 5
|
|
68
|
+ done
|
|
69
|
+}
|
|
70
|
+
|
|
71
|
+for h in $(cat "$HOSTS_FILES")
|
|
72
|
+do
|
|
73
|
+ printf "%30s %8s %30s\n" $h "unknw" "unknwn" >> $res_file
|
|
74
|
+done
|
|
75
|
+
|
|
76
|
+for h in $(cat "$HOSTS_FILES" )
|
|
77
|
+do
|
|
78
|
+ upd_ping $h $res_file $lock &
|
|
79
|
+ pid=$!
|
|
80
|
+ pids="${pids}\n$pid"
|
|
81
|
+ echo "$h updated by $pid" >&2
|
|
82
|
+done
|
|
83
|
+sleep 1
|
|
84
|
+
|
|
85
|
+while [ 1 ]
|
|
86
|
+do
|
|
87
|
+ semlock $lock
|
|
88
|
+ clear
|
|
89
|
+ echo "Ping radar $($date_cmd)\n"
|
|
90
|
+ printf "%30s %8s %30s\n" "host" "ping" "last up"
|
|
91
|
+ for i in $(seq 70); do echo -n "="; done
|
|
92
|
+ echo ""
|
|
93
|
+
|
|
94
|
+ #cat $res_file
|
|
95
|
+
|
|
96
|
+ #grep -v " down " $res_file
|
|
97
|
+ #grep " down " $res_file
|
|
98
|
+
|
|
99
|
+ sed -E -e "s/\snever$/$(tput sgr0)\0/" -e "s/^.* down .*$/$(tput setaf 1)\0$(tput sgr0)/" $res_file
|
|
100
|
+
|
|
101
|
+ semrelease $lock
|
|
102
|
+ sleep 1
|
|
103
|
+done
|