-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp_monitor_json.sh
98 lines (83 loc) · 2.23 KB
/
ftp_monitor_json.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
readonly script_path=$(readlink -f $0)
readonly base_dir=$(dirname $script_path)
# get file name without extension
base_name="${script_path##*/}"
readonly base_name="${base_name%.*}"
readonly cfg="$base_dir/$base_name.cfg"
readonly item_list=$(cat $cfg | sed '/^\s*$/d' |awk 'BEGIN {ORS = " "} {if($1 !~/^#/) print $0}'| sed -e 's/[ \t]*$//')
readonly nc_default="nc"
readonly nc_custom="/opt/IBM/tools/tivoli/nc"
function make_file() {
if [ ! -f "$1" ]; then
touch "$1" 2>/dev/null
chmod 777 "$1" 2>/dev/null
fi
}
#param1: command
function is_command() {
local result=$(whereis $1 | awk -F ":" '{print $2}' | sed '/^\s*$/d')
if [[ "$result" == "" ]]; then
echo -1
else
echo 0
fi
}
#param1: ip address
function ping_test() {
local result=$(ping -c 1 -W 1 $1 2>/dev/null| awk 'match($0, /[0-9]+% packet loss/) {print substr($0, RSTART, index($0, "%")-RSTART)}')
if [[ "$result" =~ "unknown" ]] || [[ "$result" == "" ]]; then
echo -1
else
echo $result
fi
}
#param1: host
#param2: port
#param3: nc path
function tcp_test() {
local result=$($3 -zw5 $1 $2)
if [[ "$result" == *"succeeded"* ]]; then
echo 0
else
echo -1
fi
}
function init() {
$(make_file "$cfg")
}
function main() {
$(init)
local nc_path=$nc_default
if (( $(is_command $nc_default) != 0 )); then
nc_path=$nc_custom
fi
local json_str="";
local tcp_status=""
local timestamp="$(date +"%F %T:%3N")"
if [[ "$item_list" == "" ]]; then
echo "{\"FTP\":[]}"
return 0
fi
for item in ${item_list[*]}; do
local label=$(echo $item| awk -F ';' '{print $1}')
local host=$(echo $item| awk -F ';' '{print $2}')
local port=$(echo $item| awk -F ';' '{print $3}')
if [[ "$host" == "" || "$port" == "" ]]; then
continue
fi
local result=$(tcp_test $host $port $nc_path)
if (( $result == 0 )); then
tcp_status="succeeded"
else
tcp_status="failed"
fi
json_str=$json_str"{\"label\":\"$label\",\"host\":\"$host\",\"timestamp\":\"$timestamp\",\"port\":\"$port\",\"tcp_status\":\"$tcp_status\"},"
done
json_str="{\"FTP\":["$(echo $json_str | sed 's\,$\\g')"]}"
echo $json_str
}
result=$(main)
if [[ "$result" != "" ]]; then
echo $result
fi