Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package NFrance::Report::Centreon;
- use strict;
- use feature "unicode_strings";
- use base 'Exporter';
- our @EXPORT = qw/centreon_step/;
- use utf8;
- use XML::Simple;
- use WWW::Mechanize;
- use NFrance::Report::Constants;
- use NFrance::Report::Date;
- use NFrance::Report::CLAPI;
- my $url = CENTREON_URL;
- my $ua = WWW::Mechanize->new(
- cookie_jar => {
- file => COOKIE_JAR,
- autosave => 1,
- }
- );
- #$ua->add_handler("request_send", sub { shift->dump; return});
- #$ua->add_handler("response_done", sub { shift->dump; return});
- sub get_centreon_session($$) {
- my ($user, $pass) = @_;
- my $surl = "$url/main.php?p=1&autologin=1&useralias=joris&token=zDFkezAe6";
- $ua->get($surl);
- $ua->cookie_jar->save(COOKIE_JAR);
- }
- sub get_centreon_checks($$) {
- my ($machine, $sid) = @_;
- my $surl = "$url/include/monitoring/status/Services/xml/serviceXML.php";
- $surl = "$surl?&search=&search_host=$machine&limit=5000&criticality=0 ";
- $ua->get($surl);
- print Dumper $ua;
- my $checks = XMLin $ua->content;
- my @res;
- foreach my $c (@{$checks->{l}}) {
- my $h;
- #$h->{color} = $c->{'sc'},
- $h->{check} = $c->{'sdl'};
- $h->{status} = $c->{'cs'};
- $h->{since} = $c->{'last_hard_state_change'};
- $h->{message} = $c->{'po'};
- $h->{color} = 'lightgreen' if $h->{status} eq 'OK';
- $h->{color} = 'orange' if $h->{status} eq 'WARNING';
- $h->{color} = 'red' if $h->{status} eq 'CRITICAL';
- $h->{color} = 'lightgrey' if $h->{status} eq 'UNKNOWN';
- push (@res, $h);
- }
- return \@res;
- }
- sub get_centreon_report($$) {
- my ($group, $sid) = @_;
- my $surl = "$url/include/reporting/dashboard/csvExport/csv_HostGroupLogs.php";
- $surl = "$surl?sid=$sid&hostgroup=$group";
- $surl = "$surl&end=".date_get_end_epoch;
- $surl = "$surl&start=".date_get_start_epoch;
- print "$surl\n";
- $ua->get($surl);
- my $csv = $ua->content();
- my $global;
- my $status;
- my $hosts;
- my $report;
- my $alerts;
- foreach my $line (split /\n/, $csv) {
- if ($line =~ /Hostgroup/) { $global = 1; next; }
- if ($line =~ /Status/) { $status = 1; next; }
- if ($line =~ /Hosts /) { $hosts = 1; next; }
- if ($line =~ /^$/) {
- $global++ if $global;
- $status++ if $status;
- $hosts++ if $hosts;
- }
- if ($global and $global < 2) {
- my ($group, $begin, $end, $duration) = split /;/, $line;
- $report->{group} = $group;
- $report->{begin} = $begin;
- $report->{end} = $end;
- $report->{duration} = $duration;
- next;
- }
- if ($status and $status < 2) {
- my ($state, $total, $mean_total, $alert) = split /;/, $line;
- $state = lc $state;
- $report->{$state}->{total} = $total;
- $report->{$state}->{alert} = $alert;
- $report->{$state}->{mean_total} = $mean_total;
- next;
- }
- if ($hosts and $hosts < 2) {
- my ($name, $up, $upm, $upa, $do, $dom, $doa, $ur, $urm, $ura, $un) = split /;/, $line;
- $alerts = $upa + $doa + $ura;
- $report->{hosts}->{$name}->{up} = $up;
- $report->{hosts}->{$name}->{upm} = $upm;
- $report->{hosts}->{$name}->{upa} = $upa;
- $report->{hosts}->{$name}->{do} = $do;
- $report->{hosts}->{$name}->{dom} = $dom;
- $report->{hosts}->{$name}->{doa} = $doa;
- $report->{hosts}->{$name}->{ur} = $ur;
- $report->{hosts}->{$name}->{urm} = $urm;
- $report->{hosts}->{$name}->{ura} = $ura;
- $report->{hosts}->{$name}->{un} = $un;
- next;
- }
- last if ( $status and
- $status > 1 and
- $hosts and
- $hosts > 1 and
- $global and
- $global > 1)
- }
- use Data::Dumper;
- if ($alerts and $alerts > 0) {
- $surl = "$url/include/eventLogs/GetXmlLog.php";
- $surl = "$surl?multi=1&oh=false&warning=true&unknown=true&critical=true";
- $surl = "$surl&ok=false&unreachable=true&down=true&up=false&num=0";
- $surl = "$surl&error=true&alert=true¬ification=true&search_H=&search_S=&period=";
- $surl = "$surl&StartDate=".date_fXXXing_centreon_start;
- $surl = "$surl&EndDate=".date_fXXXing_centreon_end;
- $surl = "$surl&StartTime=00:00&EndTime=23:59";
- $surl = "$surl&id=HG_$group&sid=$sid";
- $ua->get($surl);
- $alerts = XMLin($ua->content);
- }
- if ($alerts) {
- $alerts = $alerts->{line} if $alerts;
- unless (ref($alerts) eq 'ARRAY') {
- my @a;
- push @a, $alerts;
- $alerts = \@a;
- }
- foreach my $a (@{$alerts}) {
- $a->{service_description} = "" if ref $a->{service_description} eq 'HASH';
- my $output = $a->{output};
- utf8::decode($output);
- $a->{output} = $output;
- }
- }
- $report->{alerts} = $alerts;
- return $report;
- }
- sub centreon_step($) {
- my $report = shift;
- my $sid = get_centreon_session($report->{centreon_user},
- $report->{centreon_pass});
- # Set groupids
- my $h;
- foreach my $key (keys %{$report->{apps}}) {
- my $app = $report->{apps}->{$key};
- my $group = $app->{centreon_group};
- $h->{$group}->{id} = get_group_id($group,
- $report->{centreon_user},
- $report->{centreon_pass},);
- }
- foreach my $g (@{$report->{centreon_extra_groups}}) {
- $h->{$g}->{id} = get_group_id($g,
- $report->{centreon_user},
- $report->{centreon_pass},);
- }
- $report->{groupids} = $h;
- # Get reports
- foreach my $key (keys %{$report->{groupids}}) {
- my $group = $report->{groupids}->{$key};
- $group->{report} = get_centreon_report(
- $group->{id}, $sid);
- }
- # Get checks
- foreach my $m (keys %{$report->{machines}}) {
- my $h = $report->{machines}->{$m};
- $h->{checks} = get_centreon_checks(
- $h->{hostname}, $sid);
- }
- return $report;
- }
- 1;
- #curl 'http://nagios1.nfrance.com/centreon/include/eventLogs/GetXmlLog.php?multi=1&oh=false&warning=true&unknown=true&critical=true&ok=false&unreachable=true&down=true&up=false&num=0&error=true&alert=true¬ification=true&search_H=&search_S=&period=15552000&StartDate=06/01/2015&EndDate=06/30/2015&StartTime=00:00&EndTime=00:00&id=HG_236&sid=8nd225cjhie67qjog4u9jb6m37' -H 'Host: nagios1.nfrance.com' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.7.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' -H 'Accept-Encoding: gzip, deflate' -H 'Referer: http://nagios1.nfrance.com/centreon/main.php?p=203' -H 'Cookie: PHPSESSID=8nd225cjhie67qjog4u9jb6m37' -H 'Connection: keep-alive'
- =begin
- Hostgroup;Begin date; End date; Duration
- cutm_culture; 01/06/2015 00:00:00; 30/06/2015 00:00:00; 2505600s
- Status;Total Time;Mean Time; Alert
- DOWN;0;0%;0;
- UP;100;100%;1;
- UNREACHABLE;0;0%;0;
- UNDETERMINED;0;
- Hosts Group;Up Time;Up Mean Time;Up Alerts;Down Time;Down Mean Time;Down Alerts;Unreachable Time;Unreachable Mean Time;Unreachable Alerts;Undetermined Time;
- cu-prod-front1.cutm.nfrance.com;100;100;1;0;0;0;0;0;0;0;
- =cut
- # http://nagios1.nfrance.com/centreon/include/reporting/dashboard/csvExport/csv_HostGroupLogs.php?sid=gpfiht3gr7ishobvq5olv0h396&hostgroup=231&start=1433109600&end=1434492000
- =begin
- 'svc_id' => '113',
- 'hnl' => 'tm-prod-web1.cutm.nfrance.com',
- 'sc' => '#13EB3A',
- 'pa' => '0',
- 'hci' => '0',
- 'last_hard_state_change' => '1M 2d 16h 4m 58s',
- 'class' => 'list_one',
- 'eh' => '1',
- 'sdl' => 'ssh',
- 'hpe' => '0',
- 'hc' => 'transparent',
- 'hae' => '1',
- 'dtm' => '0',
- 'nc' => '17/06/2015 13:38:11',
- 'ne' => '1',
- 'sau' => 'none',
- 'hid' => '1520',
- 'ackXsl' => './include/monitoring/acknowlegement/xsl/popupForAck.xsl',
- 'ca' => '1/3 (H)',
- 'po' => 'SSH OK - OpenSSH_6.6.1 (protocol 2.0)',
- 'd' => '1M 2d 16h 4m 58s',
- 'sico' => {},
- 'sn' => 'none',
- 'pc' => '0',
- 'hs' => '0',
- 'dtmXsl' => './include/monitoring/downtime/xsl/popupForDowntime.xsl',
- 'is' => '0',
- 'fd' => '1',
- 'cs' => 'OK',
- 'ppd' => '1',
- 'ackXml' => './include/monitoring/acknowlegement/xml/broker/makeXMLForAck.php?sid=u4lhn1edd3t6obdib2j0p5d0o5&hid=1520&svc_id=113',
- 'o' => '7',
- 'lc' => '17/06/2015 13:23:11',
- 'snn' => 'none',
- 'sd' => 'ssh',
- 'ha' => '0',
- 'rd' => '2860441',
- 'hn' => {
- 'none' => '1',
- 'content' => 'tm-prod-web1.cutm.nfrance.com'
- },
- 'dtmXml' => './include/monitoring/downtime/xml/broker/makeXMLForDowntime.php?sid=u4lhn1edd3t6obdib2j0p5d0o5&hid=1520&svc_id=113',
- 'snu' => 'none',
- 'svc_index' => '29007',
- 'ac' => '1'
- =cut
- #curl --silent 'http://nagios1.nfrance.com/centreon/include/monitoring/status/Services/xml/broker/serviceXML.php?&sid=go18dsd7m65dq9i5ulfvn73c14&search&search=&search_host&criticality=0&limit=5000' -H 'Cookie: PHPSESSID=go18dsd7m65dq9i5ulfvn73c14' |xmllint --format - |grep sdl | wc -l
- #curl -i 'http://nagios1.nfrance.com/centreon/include/monitoring/status/Common/updateContactParamHostGroups.php?uid=26&hostgroups=305,306' -H 'Cookie: PHPSESSID=go18dsd7m65dq9i5ulfvn73c14'
- # http://nagios1.nfrance.com/centreon/include/reporting/dashboard/csvExport/csv_HostGroupLogs.php?sid=gpfiht3gr7ishobvq5olv0h396&hostgroup=231&start=1433109600&end=1434492000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement