-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzabbix_sender.sh
executable file
·110 lines (103 loc) · 2.69 KB
/
zabbix_sender.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
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/sh
#
### Zabbix sender implementation in bourne shell
#
# Requires: nc, cut
usage() {
echo -n "Usage:
zabbix_sender.sh -z server [-p port] -s host -k key -o value
zabbix_sender.sh -z server [-p port] -i input-file"
exit 1
}
# Check if nc is available
if ! which nc >/dev/null; then
echo "nc is not avaiable, cannot continue"
exit 1
fi
# Check first if any arguments were provided
if [ $# -eq 0 ]; then
usage
fi
# Put the arguments in variables
while [ $# -gt 0 ]; do
case "$1" in
-z|--zabbix-server)
serverHost="$2"
shift 2
;;
-p|--port)
serverPort="$2"
shift 2
;;
-s|--host)
clientHost="$2"
shift 2
;;
-k|--key)
itemKey="$2"
shift 2
;;
-o|--value)
itemValue="$2"
shift 2
;;
-i|--input-file)
inputFile="$2"
shift 2
;;
*)
echo help
exit 1
;;
esac
done
# Check if server was specified
if [ -z "$serverHost" ]; then
echo "Server hostname or IP must be specified"
echo ""
usage
fi
# Check if port was specified, if not, set to default
if [ -z "$serverPort" ]; then
serverPort="10051"
fi
# Check if input file or key/value pair was specified
if [ -n "$inputFile" ]; then
# Begin JSON data
data='{"request":"sender data","data":['
# Iterate over all lines in $inputFile and add the items to the JSON data
while read line; do
data=$data'{"host":'\"$(echo $line | cut -d' ' -f1)\"',"key":'\"$(echo $line | cut -d' ' -f2)\"',"value":'\"$(echo $line | cut -d' ' -f3-)\"'},'
done < $inputFile
# Remove last , and end JSON
data=${data%?}']}'
# Build header - https://www.zabbix.com/documentation/4.0/manual/appendix/protocols/header_datalen
length=$(printf '%016x' "${#data}")
pack=""
for i in 15 13 11 9 7 5 3 1; do
tmp=$(echo $length | cut -b $i-$(($i+1)))
pack="$pack\\x$tmp"
done
echo "Server query: $(printf "ZBXD\\1$pack%s" "$data")"
# Send it to the server
response=$(printf "ZBXD\\1$pack%s" "$data" | nc $serverHost $serverPort)
echo "Server response: $response"
elif [ -n "$itemKey" ] && [ -n "$itemValue" ]; then
# Build JSON data
data='{"request":"sender data","data":[{"host":'\"$clientHost\"',"key":'\"$itemKey\"',"value":'\"$itemValue\"'}]}'
# Build header - https://www.zabbix.com/documentation/4.0/manual/appendix/protocols/header_datalen
length=$(printf '%016x' "${#data}")
pack=""
for i in 15 13 11 9 7 5 3 1; do
tmp=$(echo $length | cut -b $i-$(($i+1)))
pack="$pack\\x$tmp"
done
echo "Server query: $(printf "ZBXD\\1$pack%s" "$data")"
# Send it to the server
response=$(printf "ZBXD\\1$pack%s" "$data" | nc $serverHost $serverPort)
echo "Server response: $response"
else
echo "Either input file or key/value pair must be specified"
echo ""
usage
fi