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
| #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);
|