-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestQuery_MADBSM2.sh
107 lines (84 loc) · 3.26 KB
/
RestQuery_MADBSM2.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
#!/bin/bash
# readme:
# usage: ./RestQuery_MADBSM2.sh <IP>
# output: one json file per host id wich is mapped to the Template MADBSM
# more info at https://confluence.int.net.nokia.com/display/RSCON/Zabbix
function pause(){
read -p "$*"
}
#USER LOGIN
#To be improved and only do the user login when failing the query
a=$(curl -s -X POST -H 'Content-Type: application/json' -d '
{
"jsonrpc": "2.0","method": "user.login",
"params": {"user": "Admin","password": "zabbix"},
"id": 1,"auth": null
}' http://$1/zabbix/api_jsonrpc.php | python -mjson.tool | grep -oPw '(?<=result":\s")[^"]+')
echo login to $1 done with result $a
#pause 'Press [Enter] key to continue...'
#FINDS THE TEMPLATE ID
t=$(curl -s -X POST -H 'Content-Type: application/json' -d '
{
"jsonrpc": "2.0", "method": "template.get",
"params": {"output": ["host"],
"filter": {"host": ["MADBSM template"]}
},"id": 1, "auth": "'$a'"
}' http://$1/zabbix/api_jsonrpc.php | python -mjson.tool | grep -oP '(?<=templateid":\s")[0-9]+')
echo TemplateID is $t
#pause 'Press [Enter] key to continue...'
#LISTS HOSTIDS, HOSTNAMES AND IPS OF THE HOSTS MAPPED TO THE TEMPLATE
curl -s -X POST -H 'Content-Type: application/json' -d '
{
"jsonrpc": "2.0", "method": "host.get",
"params": {"output": ["hostid", "host", "itemids"],"selectInterfaces": ["ip"], "templateids":"'$t'"},
"id": 1,"auth": "'$a'"
}' http://$1/zabbix/api_jsonrpc.php | python -mjson.tool > RestQuery_Template_MADBSM_hosts_list_$1.json
hostlist=$(grep -oP '(?<=hostid":\s")[0-9]+' RestQuery_Template_MADBSM_hosts_list_$1.json)
echo Hoslist $hostlist
echo $hostlist > hostlist
#itemlist=$(grep -oP '(?<=itemid":\s")[0-9]+' RestQuery_Template_MADBSM_hosts_list_$1.json)
#echo ItemIdslist $itemlist
#echo $itemlist > itemlist
#COUNTS THE NUMBER OF HOSTS AND TRANSFORMS THE LIST INTO AN ARRAY
declare -a h
l=$(wc -w hostlist | awk '{print $1}')
echo There are $l hosts
for ((i=1; i<$l+1; i++)); do h[i]=$(awk '{print $'$i'}' hostlist) ; done
#pause 'Press [Enter] key to continue...'
# TEST output for checking the new array ${h[*]}
#for (( i=0; i<$l+1; i++ )); do echo hostlist[$i] ${hostlist[$i]}; done
#for (( i=1; i<$l+1; i++ )); do echo h[$i] ${h[$i]}; done
#pause 'Press [Enter] key to continue...'
#COUNTS THE NUMBER OF ITEMS AND TRANSFORMS THE LIST INTO AN ARRAY
#declare -a items
#l=$(wc -w itemlist | awk '{print $1}')
#echo There are $l items
#for ((i=1; i<$l+1; i++)); do items[i]=$(awk '{print $'$i'}' itemlist) ; done
#QUERIES THE SERVER FOR ALL ITEMS OF EACH HOSTID
for (( i=1; i<$l+1; i++ )); do (
curl -s -X POST -H 'Content-Type: application/json' -d '
{
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output":
[
"hostid",
"itemid",
"snmp_oid",
"name",
"key_",
"lastvalue"
],
"hostids": "'${h[i]}'",
"sortfield": "key_"
},
"id": 1,
"auth": "'$a'"
}' http://$1/zabbix/api_jsonrpc.php | python -mjson.tool > RestQuery_Template_MADBSM_$1_hostid_${h[i]}.json;
echo Returned all itemids, oids, names and values of the hostid in the file RestQuery_Template_MADBSM_$1_hostid_${h[i]}.json); done
hist=300
now=$(date +%s)
echo time now $now
before=$(date --date="-$hist seconds" +"%s")
echo before $before