-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathzapish.inc
161 lines (138 loc) · 3.07 KB
/
zapish.inc
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# zapish - Zabbix API SHell binding
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2.
#
# Copyright (C) 2017-2018 Tomasz Kłoczko <kloczek@fedoraproject.org>
#
zapish_url=""
zapish_auth=""
zapish_request=""
json_str() {
echo -n "\"$1\":\"$2\""
shift 2
while [ -n "$1" ]; do
echo -n ",$1"
shift 1
done
}
json_num() {
echo -n "\"$1\":$2"
shift 2
while [ -n "$1" ]; do
echo -n ",$1"
shift 1
done
}
json_list() {
echo -n "\"$1\":{$2"
shift 2
while [ -n "$1" ]; do
echo -n ",$1"
shift 1
done
echo -n "}"
}
json_array_num() {
echo -n "\"$1\":[$2"
shift 2
while [ -n "$1" ]; do
echo -n ",$1"
shift 1
done
echo -n "]"
}
json_array_str() {
echo -n "\"$1\":[\"$2\""
shift 2
while [ -n "$1" ]; do
echo -n ",\"$1\""
shift 1
done
echo -n "]"
}
#====================================
# Zabbix result processor
#====================================
json_get() {
# for i in $(eval "echo {1..${#1}}"); do
# echo -n "${1:$((i-1)):1} "
# done
echo "$1" | jq "$2"
}
json_error() {
if [ "$(json_get "${1}" .error.code)" != "null" ]; then
json_get "${1}" .error
exit 4
else
echo "$1"
fi
}
#====================================
# Zabbix API caller
#====================================
zabbix_api() {
if [ -z "$1" ]; then
echo "zabbix_api() function expects a list of parameters describing API call"
exit 3
fi
zapish_request="{$(json_str jsonrpc "2.0" \
"$(json_str method "$1" \
"$2" \
"$(json_str auth $zapish_auth "")" \
"$(json_num id 0 "")" \
"")" \
)}"
json_error "$(curl --silent -X POST -H \
'Content-Type: application/json' -d \
"$zapish_request" $zapish_url)"
}
#====================================
# Check and initialize authentication
#====================================
zapish_init() {
echo -n "Enter URL of the Zabbix API gateway [http://localhost/api_jsonrpc.php]: "
read zapish_url
if [ ! -n "$zapish_url" ]; then
zapish_url="http://localhost/api_jsonrpc.php"
fi
echo -n "Enter Zabbix API account name [Admin]: "
read zapish_user
if [ ! -n "$zapish_user" ]; then
zapish_user="Admin"
fi
echo -n "Enter Zabbix API account password [zabbix]: "
read zapish_pwd
if [ ! -n "$zapish_pwd" ]; then
zapish_pwd="zabbix"
fi
zapish_request="{""$(json_str jsonrpc "2.0" \
$(json_str method user.login \
$(json_list params \
$(json_str user "$zapish_user" \
"") \
$(json_str password "$zapish_pwd" \
"") \
"") \
$(json_num id 0 \
"") \
))""}"
zapish_result="$(curl --silent -X POST -H \
'Content-Type: application/json' -d "${zapish_request}" ${zapish_url})"
if [ "$(json_get "${zapish_result}" .error )" != "null" ]; then
echo "Zabbix API authentication error."
exit 1
fi
echo zapish_url=\"$zapish_url\" > ~/.zapish.rc
echo zapish_auth=$(json_get "${zapish_result}" .result) >> ~/.zapish.rc
chmod 600 ~/.zapish.rc
echo "Zabbix API authentication successful. No Zabbix API call."
echo "zapish authentication token has been stored in ~/.zapish.rc"
exit 2
}
# initialization
if [ -f ~/.zapish.rc ]; then
. ~/.zapish.rc
else
zapish_init
fi