Advertisement
ileshwar

list_items.pl

Aug 28th, 2014
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use JSON;
  3.  
  4.  
  5. # send json and get result (jsonstr):jsonstr
  6. sub sendjson
  7. {
  8. #jsonstr
  9. $jsonstr = $_[0];
  10.  
  11. # send json to zabbix and get result
  12. $res = `curl -s -i -X POST -H $header -d '$data' $url`;
  13. # find start of json
  14. $i = index($res, "{");
  15. # get json only
  16. $res_out = substr($res, $i);
  17.  
  18. #return
  19. return $res_out;
  20. }
  21.  
  22.  
  23. # authenticate with zabbix, returns the auth token
  24. sub authenticate
  25. {
  26. # load auth json
  27. $data = '{ "jsonrpc": "2.0", "method": "user.authenticate", "params": { "user": "'.$user.'", "password": "'.$password.'" }, "id": 0, "auth": null }';
  28. # send json
  29. $res = sendjson($data);
  30.  
  31. # decode json
  32. $dec = decode_json($res);
  33. # get auth key
  34. $auth_out = $dec->{"result"};
  35.  
  36. #return
  37. return $auth_out;
  38. }
  39.  
  40.  
  41. # get hostgroups from zabbix (auth)
  42. sub gethostgroups
  43. {
  44. #auth
  45. $auth_in = $_[0];
  46.  
  47. # load hostgroups json
  48. $data = '{ "jsonrpc": "2.0", "method": "hostgroup.get", "params": { "output": "extend", "sortfield": "name" }, "id": 1, "auth": "" }';
  49. # decode json
  50. $dec = decode_json($data);
  51. # set auth
  52. $dec->{"auth"} = $auth_in;
  53. # encode back to data
  54. $data = encode_json($dec);
  55.  
  56. # send json
  57. $res = sendjson($data);
  58. # decode json
  59. $dec_out = decode_json($res);
  60.  
  61. #return
  62. return $dec_out
  63. }
  64.  
  65.  
  66.  
  67. # get hosts from zabbix (auth, groupid)
  68. sub gethosts
  69. {
  70. #auth
  71. $auth_in = $_[0];
  72. #groupid
  73. $groupid_in = $_[1];
  74.  
  75. # load items json
  76. $data = '{ "jsonrpc": "2.0", "method": "host.get", "params": { "output": "extend", "sortfield": "name", "selectParentTemplates": "extend", "groupids": [ "" ] }, "id": 2, "auth": "" }';
  77. # decode json
  78. $dec = decode_json($data);
  79. # set auth
  80. $dec->{"auth"} = $auth_in;
  81. # set groupid filter (outside filter)
  82. $dec->{"params"}->{"groupids"}[0] = $groupid_in;
  83. # encode back to data
  84. $data = encode_json($dec);
  85.  
  86. # print $data."\n\n";
  87.  
  88. # send json
  89. $res = sendjson($data);
  90. # decode json
  91. $dec_out = decode_json($res);
  92.  
  93. # print $res."\n\n";
  94.  
  95. #return
  96. return $dec_out;
  97. }
  98.  
  99.  
  100.  
  101. # get items from zabbix (auth, hostid)
  102. sub getitems
  103. {
  104. #auth
  105. $auth_in = $_[0];
  106. #hostid
  107. $hostid_in = $_[1];
  108.  
  109. # load items json
  110. $data = '{ "jsonrpc": "2.0", "method": "item.get", "params": { "output": "extend", "sortfield": "name", "filter": { "hostid": "" } }, "id": 1, "auth": "" }';
  111.  
  112. # decode json
  113. $dec = decode_json($data);
  114. # set auth
  115. $dec->{"auth"} = $auth_in;
  116. # set hostid filter
  117. $dec->{"params"}->{"filter"}->{"hostid"} = $hostid_in;
  118. # encode back to data
  119. $data = encode_json($dec);
  120.  
  121. # print $dec."\n\n";
  122.  
  123. # send json
  124. $res = sendjson($data);
  125. # decode json
  126. $dec_out = decode_json($res);
  127.  
  128. # print $res."\n\n";
  129.  
  130. #return
  131. return $dec_out
  132. }
  133.  
  134.  
  135. #########
  136. # modify these values accordingly
  137. #########
  138. # only add graphs to hosts linked to this template
  139. $template = "WIN Windows";
  140. # internal
  141. $header = "Content-Type:application/json";
  142. # intenal zabbix url
  143. $url = "http://127.0.0.1/api_jsonrpc.php";
  144. # user
  145. $user = "Admin";
  146. # password
  147. $password = "zabbix";
  148.  
  149.  
  150. print "\n";
  151.  
  152. # authenticate with zabbix
  153. $auth = authenticate();
  154.  
  155. # get hostgroup list
  156. $hostgroups = gethostgroups($auth);
  157.  
  158. # each hostgroup in list
  159. foreach $hostgroup(@{$hostgroups->{result}}) {
  160. # get groupid and name
  161. $groupid = $hostgroup->{groupid};
  162. $name = $hostgroup->{name};
  163.  
  164. # not templates or discovered hosts
  165. if ((lc($name) ne "templates") && (lc($name) ne "discovered hosts")) {
  166.  
  167. # get hosts list
  168. $hosts = gethosts($auth, $groupid);
  169.  
  170. # each host in list
  171. foreach $host(@{$hosts->{result}}) {
  172. # get parent templates
  173. $templates = $host->{parentTemplates};
  174. # match results
  175. $templatematch = 0;
  176.  
  177. # each template in list
  178. # filter hosts that do not belong to our template
  179. foreach $templatei(@{$templates}) {
  180. # template name match
  181. if (lc($templatei->{name}) eq lc($template)) { $templatematch = 1; }
  182. }
  183.  
  184. # template match
  185. if ($templatematch == 1) {
  186. # get host id and name
  187. $name = $host->{name};
  188. $hostid = $host->{hostid};
  189.  
  190. # get item list
  191. $items = getitems($auth, $hostid);
  192.  
  193. # each item in list
  194. foreach $item(@{$items->{result}}) {
  195. # get item name
  196. $item_name = $item->{name};
  197. #get item id
  198. $item_id = $item->{itemid};
  199. #get item key
  200. $item_key = $item->{key_};
  201.  
  202. #print $item_key."\n";
  203.  
  204. if ($item_key !~ /.*{#.*}/)
  205. {
  206. print $item_name." (".$item_id.")"."\n";
  207. }
  208.  
  209.  
  210. }
  211. }
  212. }
  213. }
  214. }
  215.  
  216. print "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement