Advertisement
ileshwar

lld_all_graph.pl

Aug 28th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.81 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use JSON;
  3.  
  4. #version 2012.06.26
  5. #added item prototype exclusion by item key
  6. #now 42 colors available
  7.  
  8. #version 2012.06.14
  9. #added drawtype parameter
  10. #added calcfunction parameter
  11. #showtriggers now defaults to true
  12.  
  13. #version 2012.06.13.1
  14. #added showtriggers parameter
  15.  
  16.  
  17. # load a json string from a file
  18. sub loadjson
  19. {
  20. #reset
  21. $result = "";
  22.  
  23. # open file
  24. open(FILE, "<", $_[0]);
  25. # read whole file
  26. while (<FILE>) { $result = $result.$_; }
  27. # close file
  28. close(FILE);
  29.  
  30. #return
  31. return $result
  32. }
  33.  
  34.  
  35. # send json and get result (jsonstr):jsonstr
  36. sub sendjson
  37. {
  38. #jsonstr
  39. $jsonstr = $_[0];
  40.  
  41. # send json to zabbix and get result
  42. $res = `curl -s -i -X POST -H $header -d '$data' $url`;
  43. # find start of json
  44. $i = index($res, "{");
  45. # get json only
  46. $res_out = substr($res, $i);
  47.  
  48. #return
  49. return $res_out;
  50. }
  51.  
  52.  
  53. # authenticate with zabbix, returns the auth token
  54. sub authenticate
  55. {
  56. # load auth json
  57. $data = '{ "jsonrpc": "2.0", "method": "user.authenticate", "params": { "user": "'.$user.'", "password": "'.$password.'" }, "id": 0, "auth": null }';
  58. # send json
  59. $res = sendjson($data);
  60. "print $res;"
  61. # decode json;
  62. $dec = decode_json($res);
  63. # get auth key
  64. $auth_out = $dec->{"result"};
  65.  
  66. #return
  67. return $auth_out;
  68. }
  69.  
  70.  
  71. # get hostgroups from zabbix (auth)
  72. sub gethostgroups
  73. {
  74. #auth
  75. $auth_in = $_[0];
  76.  
  77. # load hostgroups json
  78. $data = '{ "jsonrpc": "2.0", "method": "hostgroup.get", "params": { "output": "extend", "sortfield": "name" }, "id": 1, "auth": "" }';
  79. # decode json
  80. $dec = decode_json($data);
  81. # set auth
  82. $dec->{"auth"} = $auth_in;
  83. # encode back to data
  84. $data = encode_json($dec);
  85.  
  86. # send json
  87. $res = sendjson($data);
  88. # decode json
  89. $dec_out = decode_json($res);
  90.  
  91. #return
  92. return $dec_out
  93. }
  94.  
  95.  
  96.  
  97. # get hosts from zabbix (auth, groupid)
  98. sub gethosts
  99. {
  100. #auth
  101. $auth_in = $_[0];
  102. #groupid
  103. $groupid_in = $_[1];
  104.  
  105. # load items json
  106. $data = '{ "jsonrpc": "2.0", "method": "host.get", "params": { "output": "extend", "sortfield": "name", "selectParentTemplates": "extend", "groupids": [ "" ] }, "id": 2, "auth": "" }';
  107. # decode json
  108. $dec = decode_json($data);
  109. # set auth
  110. $dec->{"auth"} = $auth_in;
  111. # set groupid filter (outside filter)
  112. $dec->{"params"}->{"groupids"}[0] = $groupid_in;
  113. # encode back to data
  114. $data = encode_json($dec);
  115.  
  116. # print $data."\n\n";
  117.  
  118. # send json
  119. $res = sendjson($data);
  120. # decode json
  121. $dec_out = decode_json($res);
  122.  
  123. # print $res."\n\n";
  124.  
  125. #return
  126. return $dec_out;
  127. }
  128.  
  129.  
  130.  
  131. # get items from zabbix (auth, hostid)
  132. sub getitems
  133. {
  134. #auth
  135. $auth_in = $_[0];
  136. #hostid
  137. $hostid_in = $_[1];
  138.  
  139. # load items json
  140. $data = '{ "jsonrpc": "2.0", "method": "item.get", "params": { "output": "extend", "sortfield": "name", "filter": { "hostid": "" } }, "id": 1, "auth": "" }';
  141.  
  142. # decode json
  143. $dec = decode_json($data);
  144. # set auth
  145. $dec->{"auth"} = $auth_in;
  146. # set hostid filter
  147. $dec->{"params"}->{"filter"}->{"hostid"} = $hostid_in;
  148. # encode back to data
  149. $data = encode_json($dec);
  150.  
  151. # send json
  152. $res = sendjson($data);
  153. # decode json
  154. $dec_out = decode_json($res);
  155.  
  156. # print $res."\n\n";
  157.  
  158. #return
  159. return $dec_out
  160. }
  161.  
  162.  
  163. # get graphs from zabbix (auth, hostid, graphname)
  164. sub getgraphs
  165. {
  166. #auth
  167. $auth_in = $_[0];
  168. #hostid
  169. $hostid_in = $_[1];
  170. #graph
  171. $graph_in = $_[2];
  172.  
  173. # load graphs json
  174. $data = '{ "jsonrpc": "2.0", "method": "graph.get", "params": { "output": "extend", "sortfield": "name", "hostids": [ "" ], "filter": { "name": "" } }, "id": 3, "auth": "" }';
  175. # decode json
  176. $dec = decode_json($data);
  177. # set auth
  178. $dec->{"auth"} = $auth_in;
  179. # set name filter
  180. $dec->{"params"}->{"filter"}->{"name"} = $graph_in;
  181. # set hostid filter (outside filter)
  182. $dec->{"params"}->{"hostids"}[0] = $hostid_in;
  183. # encode back to data
  184. $data = encode_json($dec);
  185.  
  186. # print $data."\n\n";
  187.  
  188. # send json
  189. $res = sendjson($data);
  190. # decode json
  191. $dec_out = decode_json($res);
  192.  
  193. # print $res."\n\n";
  194.  
  195. #return
  196. return $dec_out;
  197. }
  198.  
  199.  
  200.  
  201. # delete graph from zabbix (auth, graphid)
  202. sub deletegraph
  203. {
  204. #auth
  205. $auth_in = $_[0];
  206. #graphid
  207. $graphid_in = $_[1];
  208.  
  209. # load graphs json
  210. $data = '{ "jsonrpc": "2.0", "method": "graph.delete", "params": [ "" ], "id": 4, "auth": "" }';
  211. # decode json
  212. $dec = decode_json($data);
  213. # set auth
  214. $dec->{"auth"} = $auth_in;
  215. # set graphid
  216. $hash[0] = $graphid_in;
  217. $dec->{"params"} = \@hash;
  218. # encode back to data
  219. $data = encode_json($dec);
  220.  
  221. # print $data."\n\n";
  222.  
  223. # send json
  224. $res = sendjson($data);
  225. # decode json
  226. $dec_out = decode_json($res);
  227.  
  228. # print $res."\n\n";
  229.  
  230. print " "." "." "."Graph deleted."."\n";
  231.  
  232. #return
  233. return $dec_out;
  234. }
  235.  
  236.  
  237. # search and delete existing graphs from zabbix (auth, hostid, graphname)
  238. sub deletegraphs
  239. {
  240. #auth
  241. $auth_in = $_[0];
  242. #hostid
  243. $hostid_in = $_[1];
  244. #graph
  245. $graph_in = $_[2];
  246.  
  247.  
  248. # get graph with name
  249. $graphs = getgraphs($auth_in, $hostid_in, $graph_in);
  250.  
  251. # each graph in list
  252. # filter graphs that do not belong to our hostid
  253. foreach $graphi(@{$graphs->{result}}) {
  254.  
  255. # get graph id
  256. $graphid = $graphi->{graphid};
  257. # get graph name
  258. $graph_name = $graphi->{name};
  259.  
  260. print " "." "." "."Graph found: ".$graph_name." (".$graphid.")"."\n";
  261.  
  262. # delete the graph
  263. deletegraph($auth_in, $graphid);
  264. }
  265. }
  266.  
  267.  
  268. # create graph in zabbix (auth, graphname, graphtype, mintype, maxtype, minvalue, maxvalue, showtriggers, graphitems, hostid)
  269. sub creategraph
  270. {
  271. #ymin_type = 0 -> calculated
  272. #ymin_type = 1 -> fixed
  273. #graphtype = 0 -> normal
  274. #graphtype = 1 -> stack
  275.  
  276. #auth
  277. $auth_in = $_[0];
  278. #graph name
  279. $graph_in = $_[1];
  280. #graphtype, mintype, maxtype, minvalue, maxvalue
  281. $graphtype_in = $_[2];
  282. $mintype_in = $_[3];
  283. $maxtype_in = $_[4];
  284. $minvalue_in = $_[5];
  285. $maxvalue_in = $_[6];
  286. $showtriggers_in = $_[7];
  287. #graphitems
  288. $graphitems_in = $_[8];
  289. #hostid
  290. $hostid_in = $_[9];
  291.  
  292. # load graphs json
  293. $data = '{ "jsonrpc": "2.0", "method": "graph.create", "params": { "gitems": [ "" ], "name": "", "width": "900", "height": "300", "yaxismin": "0", "yaxismax": "100",
  294. "show_work_period": "1", "show_triggers": "1", "graphtype": "0", "show_legend": "1", "show_3d": "0", "percent_left": "0", "percent_right": "0", "ymin_type": "0",
  295. "ymax_type": "0", "ymin_itemid": "0", "ymax_itemid": "0" }, "id": 4, "auth": "" }';
  296.  
  297. # decode json
  298. $dec = decode_json($data);
  299. # set auth
  300. $dec->{"auth"} = $auth_in;
  301. # set graph name
  302. $dec->{"params"}->{"name"} = $graph_in;
  303. # set graphtype, mintype, maxtype, minvalue, maxvalue
  304. $dec->{"params"}->{graphtype} = $graphtype_in;
  305. $dec->{"params"}->{ymin_type} = $mintype_in;
  306. $dec->{"params"}->{ymax_type} = $maxtype_in;
  307. $dec->{"params"}->{yaxismin} = $minvalue_in;
  308. $dec->{"params"}->{yaxismax} = $maxvalue_in;
  309. $dec->{"params"}->{show_triggers} = $showtriggers_in;
  310. # set graph gitems
  311. $dec->{"params"}->{gitems} = $graphitems_in;
  312. # encode back to data
  313. $data = encode_json($dec);
  314.  
  315. # print $data."\n\n";
  316.  
  317. # send json
  318. $res = sendjson($data);
  319. # decode json
  320. $dec_out = decode_json($res);
  321.  
  322. # print $res."\n\n";
  323.  
  324. print " "." "." "."Graph created: ".$graph_in."\n";
  325.  
  326. #return
  327. return $dec_out;
  328. }
  329.  
  330.  
  331.  
  332. #########
  333. # modify these values accordingly
  334. #########
  335. # user
  336. $user = "Admin";
  337. # password
  338. $password = "zabbix";
  339. # only add graphs to hosts linked to this template
  340. $template = "WIN Processes";
  341. # internal
  342. $header = "Content-Type:application/json";
  343. # intenal zabbix url
  344. $url = "http://127.0.0.1/api_jsonrpc.php";
  345. # create a graph with this name in each host
  346. $graph = 'WIN Volume "ALL" bytes/sec stack';
  347. $graphtype = 1; ### 0=normal, 1=stacked
  348. $mintype = 1; ### 0=calculated, 1=fixed
  349. $maxtype = 0; ### 0=calculated, 1=fixed
  350. $minvalue = 0;
  351. $maxvalue = 100;
  352. $showtriggers = 1;
  353. $drawtype = 0; ### 0=line, 1=filled, 2=boldline, 3=dot, 4=dashed, 5=gradient
  354. $calcfunction = 2; ### 1=min, 4=max, 2=avg, 7=all
  355. # add graph items mathing these regexes, maximum 2
  356. $regexes[0] = '^WIN\sVolume\s".*"\sbytes/sec\swrite';
  357. $regexes[1] = '^WIN\sVolume\s".*"\sbytes/sec\sread';
  358.  
  359.  
  360.  
  361. ##########
  362. # 42 total items
  363. ##########
  364. # dark colors
  365. $colors[0][0] = "5299AD"; # blue1
  366. $colors[0][1] = "5D549A"; # violet
  367. $colors[0][2] = "87B457"; # green
  368. $colors[0][3] = "CF545E"; # red
  369. $colors[0][4] = "CDDA13"; # lemon
  370. $colors[0][5] = "5DAE99"; # turquise
  371. $colors[0][6] = "DD844C"; # orange
  372. $colors[0][7] = "AE5C8A"; # mauve
  373. $colors[0][8] = "BD9F83"; # ltbrown
  374. $colors[0][9] = "6B9BD4"; # blue2
  375. $colors[0][10] = "B75F73"; #red-brown
  376. $colors[0][11] = "646560"; # kaky
  377. $colors[0][12] = "335098"; # deepblue
  378. $colors[0][13] = "5FBFDB"; # bleu
  379. $colors[0][14] = "D1CE85"; # yellow
  380. $colors[0][15] = "909090"; # grey
  381. $colors[0][16] = "A16254"; # brown
  382. $colors[0][17] = "E8678D"; # pink
  383. $colors[0][18] = "62B55A"; # deepgreen
  384. $colors[0][19] = "A599AD"; # greypurple
  385. $colors[0][20] = "6A5DD9"; # violet2
  386. # light colors
  387. $colors[1][0] = "98D6E7"; # blue1
  388. $colors[1][1] = "9E7EDF"; # violet
  389. $colors[1][2] = "BDDA83"; # green
  390. $colors[1][3] = "EF747E"; # red
  391. $colors[1][4] = "EDFA33"; # lemon
  392. $colors[1][5] = "7EC392"; # tuquise
  393. $colors[1][6] = "EDA46C"; # orange
  394. $colors[1][7] = "DF93D7"; # mauve
  395. $colors[1][8] = "E2BB91"; # ltbrown
  396. $colors[1][9] = "A0CBEA"; # blue2
  397. $colors[1][10] = "CB868B"; # red-brown
  398. $colors[1][11] = "77897D"; # kaky
  399. $colors[1][12] = "5370B8"; #deepblue
  400. $colors[1][13] = "76DAF7"; # bleu
  401. $colors[1][14] = "EAD770"; # yellow
  402. $colors[1][15] = "AEAEAE"; # grey
  403. $colors[1][16] = "B97A6F"; # brown
  404. $colors[1][17] = "E8849D"; # pink
  405. $colors[1][18] = "95D36E"; # deepgreen
  406. $colors[1][19] = "B7AACF"; # greypurple
  407. $colors[1][20] = "8A7DF9"; # violet2
  408.  
  409.  
  410.  
  411. print "\n";
  412.  
  413. # authenticate with zabbix
  414. $auth = authenticate();
  415.  
  416. # get hostgroup list
  417. $hostgroups = gethostgroups($auth);
  418.  
  419. # each hostgroup in list
  420. foreach $hostgroup(@{$hostgroups->{result}}) {
  421. # get groupid and name
  422. $groupid = $hostgroup->{groupid};
  423. $name = $hostgroup->{name};
  424.  
  425. # not templates or discovered hosts
  426. if ((lc($name) ne "templates") && (lc($name) ne "discovered hosts")) {
  427.  
  428. # get hosts list
  429. $hosts = gethosts($auth, $groupid);
  430.  
  431. print "HOSTGROUP: ".$name." (".$groupid.")"."\n";
  432.  
  433. # each host in list
  434. foreach $host(@{$hosts->{result}}) {
  435. # get parent templates
  436. $templates = $host->{parentTemplates};
  437. # match results
  438. $templatematch = 0;
  439.  
  440. # each template in list
  441. # filter hosts that do not belong to our template
  442. foreach $templatei(@{$templates}) {
  443. # template name match
  444. if (lc($templatei->{name}) eq lc($template)) { $templatematch = 1; }
  445. }
  446.  
  447. # template match
  448. if ($templatematch == 1) {
  449. # get host id and name
  450. $name = $host->{name};
  451. $hostid = $host->{hostid};
  452. # reset graph item array
  453. @graph_item = ();
  454.  
  455. print " "."HOST: ".$name." (".$hostid.")"."\n";
  456.  
  457. #########
  458. # search for existing graphs and delete if found
  459. #########
  460. deletegraphs($auth, $hostid, $graph);
  461.  
  462. # get item list
  463. $items = getitems($auth, $hostid);
  464. $count = 0;
  465. # reset colorbase;
  466. $colorbase = 0;
  467. # reset colorindexes;
  468. @colorindex = 0;
  469.  
  470. # each item in list
  471. foreach $item(@{$items->{result}}) {
  472. # get item name
  473. $item_name = $item->{name};
  474. #get item id
  475. $item_id = $item->{itemid};
  476. #get item key
  477. $item_key = $item->{key_};
  478. # reset regex index
  479. $regexindex = 0;
  480. # reset host item array
  481. @host_item = ();
  482.  
  483. # each regex in list
  484. foreach $regex(@regexes) {
  485.  
  486. # item name match regex and item key is not prototype
  487. if (($item_name =~ /$regex/) && ($item_key !~ /.*{#.*}/))
  488. {
  489. print " "." "."ITEM: ".$item_name." (".$item_id.")"."\n";
  490.  
  491. # we may have exceeded color count, if so, use other base (light, ultralight)
  492. if ($colorindex[$regexindex] > $#{$colors[0]}) {
  493. # move to next color base
  494. $colorbase++;
  495. # reset color index;
  496. $colorindex[$regexindex] = 0;
  497.  
  498. print " "." "."WARNING: Not enough colors, switched to another color base"."\n";
  499. }
  500.  
  501. $graph_item[$count]->{itemid} = $item_id;
  502. $graph_item[$count]->{drawtype} = $drawtype;
  503. $graph_item[$count]->{sortorder} = $count;
  504. $graph_item[$count]->{color} = $colors[$colorbase + $regexindex][$colorindex[$regexindex]];
  505. $graph_item[$count]->{yaxisside} = "0"; ### 0=left, 1=right;
  506. $graph_item[$count]->{type} = "0";
  507. $graph_item[$count]->{calc_fnc} = $calcfunction;
  508.  
  509. # inc count;
  510. $count++;
  511. # inc colorindex;
  512. $colorindex[$regexindex]++;
  513. }
  514.  
  515. # inc regexindex
  516. $regexindex++;
  517. }
  518. }
  519.  
  520. #########
  521. # create a new graph
  522. #########
  523. creategraph($auth, $graph, $graphtype, $mintype, $maxtype, $minvalue, $maxvalue, $showtriggers, \@graph_item, $hostid);
  524. }
  525. }
  526. }
  527. }
  528.  
  529. print "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement