導出zabbix的IT service報表

zabbix中的IT service能夠是樹狀結構。php

這就要求腳本要有這個功能點:能收集到一個節點的全部子節點信息。(這是典型的遞歸循環的使用場景)。json

 

接着上篇博客,咱們已經能夠獲取到zabbix的auth信息。api

使用zbxapi_login.pl腳本獲取認證串這裏是2080444abc88afe37b59456d4b7ee0a1spa

打開頁面的IT service,點擊某個你關心的節點。之你能夠在地址欄看到以下信息。serviceID就在裏面。scala

http://x.x.x.x/zabbix/report3.php?serviceid=4&year=2016&period=weekly遞歸

 

如今我能夠用以下命令獲取到IT service信息了。第三第四個參數分別是起止時間。ci

perl getsla.pl 2080444abc88afe37b59456d4b7ee0a1 4 `date -d '2015-10-01 00:00:00' +%s` `date -d '2015-11-01 00:00:00' +%s`rpc

 

如下是腳本內容get

#cat getsla.pl 
use Text::CSV_XS; use JSON::RPC::Client; use Time::Local; use Data::Dumper; use strict; use warnings; my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/ }); sub getsla {   my($auth,$serviceid,$from,$to) = @_;    my $client = new JSON::RPC::Client;    my $uri    = 'http://zabbixIP/zabbix/api_jsonrpc.php';    my $callobj = {       jsonrpc  => '2.0',       method  => 'service.getsla',       params => {       serviceids => [$serviceid], intervals  => [{ from => "$from" , to => "$to" }], #intervals  => [ from => "1443913200" , to => "1444518000" ],          } ,      auth => "$auth",      id  => '1',    };    my $res = $client->call($uri, $callobj);    return $res->result; } sub getservice{    my($auth,$serviceid) = @_;    my $client = new JSON::RPC::Client;    my $uri    = 'http://zabbixIP/zabbix/api_jsonrpc.php';    my $callobj = {       jsonrpc  => '2.0',       method  => 'service.get',       params => {       serviceids => [$serviceid],                         selectDependencies  => "extend",                         output  => "extend",          },      auth => "$auth",      id  => '1',    };    my $res = $client->call($uri, $callobj);    my $return = $res->result;    return $return; } sub average { my (@num) = @_; my $num = scalar @num; my $total; foreach (0..$#num) {   $total += $num[$_]; } return ($total/$num); } sub getobj{ my($auth,$serviceid,$from,$to) = @_; my @array; my %hash; $hash{'sla'}=getsla($auth,$serviceid,$from,$to); $hash{'service'}=getservice($auth,$serviceid); ${$hash{'service'}}[0]{'countdependencies'}=scalar(@{$hash{'service'}->[0]{'dependencies'}}); my @num; if (${$hash{'service'}}[0]{'countdependencies'} > 0 ){ my $sid ; foreach $sid (@{$hash{'service'}->[0]{'dependencies'}}) {    my $child=&getobj($auth,$sid->{'serviceid'},$from,$to);    push (@array,$child);    push (@num,$child->{'sla'}{$child->{'service'}[0]{'serviceid'}}{'sla'}[0]{'sla'}) } my $num = scalar @num; my $total; foreach (0..$#num) { $total += $num[$_]; } $hash{'sla'}{$serviceid}{'sla'}[0]{'sla'}= $total / $num; } push (@array,\%hash); return @array; } sub caltime{   my($period) = @_;   my $days = int($period/(24*3600));   my $hours = int($period%(24*3600)/3600);   my $mins = int($period%3600/60);   my $secs = $period%60;   return $days."d,".$hours."h,".$mins."m,".$secs."s"; } open my $out,">", "/data/HTI_sla.csv" or die "Can NOT create file:outputfile:$!"; my @title=( 'Service Name','SLA','Down Time','Problem Time','OK Time','Period','from','to'); my $status = $csv->print ($out, \@title); my @ultobj; @ultobj= &getobj($ARGV[0],$ARGV[1],$ARGV[2],$ARGV[3]); my $obj; foreach $obj (@ultobj){   my @crow=();   push(@crow,$obj->{'service'}[0]{'name'});   push(@crow,$obj->{'sla'}{$obj->{'service'}[0]{'serviceid'}}{'sla'}[0]{'sla'}."%");   push(@crow,caltime($obj->{'sla'}{$obj->{'service'}[0]{'serviceid'}}{'sla'}[0]{'downtimeTime'}));   push(@crow,caltime($obj->{'sla'}{$obj->{'service'}[0]{'serviceid'}}{'sla'}[0]{'problemTime'}));   push(@crow,caltime($obj->{'sla'}{$obj->{'service'}[0]{'serviceid'}}{'sla'}[0]{'okTime'}));   push(@crow,caltime($obj->{'sla'}{$obj->{'service'}[0]{'serviceid'}}{'sla'}[0]{'to'} - $obj->{'sla'}{$obj->{'service'}[0]{'serviceid'}}{'sla'}[0]{'from'}));   my ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst) = localtime($obj->{'sla'}{$obj->{'service'}[0]{'serviceid'}}{'sla'}[0]{'from'});$year = $year+1900;$mon=$mon+1;   push(@crow,$year."-".$mon."-".$day." ".$hour.":".$min.":".$sec);   ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst) = localtime($obj->{'sla'}{$obj->{'service'}[0]{'serviceid'}}{'sla'}[0]{'to'});$year = $year+1900;$mon=$mon+1;   push(@crow,$year."-".$mon."-".$day." ".$hour.":".$min.":".$sec);   my $status = $csv->print ($out, \@crow); } #print Dumper(@ultobj);
相關文章
相關標籤/搜索