Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 8.70 KB | None | 0 0
  1. package NFrance::Report::Centreon;
  2. use strict;
  3. use feature "unicode_strings";
  4. use base 'Exporter';
  5.  
  6. our @EXPORT = qw/centreon_step/;
  7.  
  8. use utf8;
  9. use XML::Simple;
  10. use WWW::Mechanize;
  11. use NFrance::Report::Constants;
  12. use NFrance::Report::Date;
  13. use NFrance::Report::CLAPI;
  14.  
  15.  
  16.  
  17.  
  18. my $url = CENTREON_URL;
  19. my $ua = WWW::Mechanize->new(
  20.         cookie_jar => {
  21.         file => COOKIE_JAR,
  22.         autosave => 1,
  23.         }
  24.  
  25.         );
  26. #$ua->add_handler("request_send", sub { shift->dump; return});
  27. #$ua->add_handler("response_done", sub { shift->dump; return});
  28.  
  29. sub get_centreon_session($$) {
  30.     my ($user, $pass)  = @_;
  31.     my $surl = "$url/main.php?p=1&autologin=1&useralias=joris&token=zDFkezAe6";
  32.     $ua->get($surl);
  33.     $ua->cookie_jar->save(COOKIE_JAR);
  34. }
  35.  
  36. sub get_centreon_checks($$) {
  37.     my ($machine, $sid)  = @_;
  38.     my $surl = "$url/include/monitoring/status/Services/xml/serviceXML.php";
  39.     $surl = "$surl?&search=&search_host=$machine&limit=5000&criticality=0 ";
  40.     $ua->get($surl);
  41.     print Dumper $ua;
  42.     my $checks = XMLin $ua->content;
  43.     my @res;
  44.     foreach my $c (@{$checks->{l}}) {
  45.         my $h;
  46. #$h->{color}   = $c->{'sc'},
  47.         $h->{check}   = $c->{'sdl'};
  48.         $h->{status}  = $c->{'cs'};
  49.         $h->{since}   = $c->{'last_hard_state_change'};
  50.         $h->{message} = $c->{'po'};
  51.         $h->{color} = 'lightgreen' if $h->{status} eq 'OK';
  52.         $h->{color} = 'orange' if $h->{status} eq 'WARNING';
  53.         $h->{color} = 'red' if $h->{status} eq 'CRITICAL';
  54.         $h->{color} = 'lightgrey' if $h->{status} eq 'UNKNOWN';
  55.  
  56.         push (@res, $h);
  57.     }
  58.     return \@res;
  59. }
  60.  
  61. sub get_centreon_report($$) {
  62.     my ($group, $sid) = @_;
  63.  
  64.     my $surl = "$url/include/reporting/dashboard/csvExport/csv_HostGroupLogs.php";
  65.     $surl = "$surl?sid=$sid&hostgroup=$group";
  66.     $surl = "$surl&end=".date_get_end_epoch;
  67.     $surl = "$surl&start=".date_get_start_epoch;
  68.     print "$surl\n";
  69.     $ua->get($surl);
  70.     my $csv =  $ua->content();
  71.     my $global;
  72.     my $status;
  73.     my $hosts;
  74.     my $report;
  75.     my $alerts;
  76.     foreach my $line (split /\n/, $csv) {
  77.         if ($line =~ /Hostgroup/) {     $global = 1; next; }
  78.         if ($line =~ /Status/)    {     $status = 1; next; }
  79.         if ($line =~ /Hosts /)    {     $hosts  = 1; next; }
  80.         if ($line =~ /^$/) {
  81.             $global++ if $global;
  82.             $status++ if $status;
  83.             $hosts++ if $hosts;
  84.         }
  85.         if ($global and $global < 2) {
  86.             my ($group, $begin, $end, $duration) = split /;/, $line;
  87.             $report->{group} = $group;
  88.             $report->{begin} = $begin;
  89.             $report->{end} = $end;
  90.             $report->{duration} = $duration;
  91.             next;
  92.         }
  93.         if ($status and $status < 2) {
  94.             my ($state, $total, $mean_total, $alert) = split /;/, $line;
  95.             $state = lc $state;
  96.             $report->{$state}->{total} = $total;
  97.             $report->{$state}->{alert} = $alert;
  98.             $report->{$state}->{mean_total} = $mean_total;
  99.             next;
  100.         }
  101.         if ($hosts and $hosts < 2) {
  102.             my ($name, $up, $upm, $upa, $do, $dom, $doa, $ur, $urm, $ura, $un) = split /;/, $line;
  103.             $alerts = $upa + $doa + $ura;
  104.             $report->{hosts}->{$name}->{up} = $up;
  105.             $report->{hosts}->{$name}->{upm} = $upm;
  106.             $report->{hosts}->{$name}->{upa} = $upa;
  107.             $report->{hosts}->{$name}->{do} = $do;
  108.             $report->{hosts}->{$name}->{dom} = $dom;
  109.             $report->{hosts}->{$name}->{doa} = $doa;
  110.             $report->{hosts}->{$name}->{ur} = $ur;
  111.             $report->{hosts}->{$name}->{urm} = $urm;
  112.             $report->{hosts}->{$name}->{ura} = $ura;
  113.             $report->{hosts}->{$name}->{un} = $un;
  114.             next;
  115.         }
  116.         last if (   $status and
  117.                 $status > 1 and
  118.                 $hosts and
  119.                 $hosts > 1 and
  120.                 $global and
  121.                 $global > 1)
  122.     }
  123.     use Data::Dumper;
  124.     if ($alerts and $alerts > 0) {
  125.         $surl = "$url/include/eventLogs/GetXmlLog.php";
  126.         $surl = "$surl?multi=1&oh=false&warning=true&unknown=true&critical=true";
  127.         $surl = "$surl&ok=false&unreachable=true&down=true&up=false&num=0";
  128.         $surl = "$surl&error=true&alert=true&notification=true&search_H=&search_S=&period=";
  129.         $surl = "$surl&StartDate=".date_fXXXing_centreon_start;
  130.         $surl = "$surl&EndDate=".date_fXXXing_centreon_end;
  131.         $surl = "$surl&StartTime=00:00&EndTime=23:59";
  132.         $surl = "$surl&id=HG_$group&sid=$sid";
  133.         $ua->get($surl);
  134.         $alerts = XMLin($ua->content);
  135.     }
  136.     if ($alerts) {
  137.         $alerts = $alerts->{line} if $alerts;
  138.         unless (ref($alerts) eq 'ARRAY') {
  139.             my @a;
  140.             push @a, $alerts;
  141.             $alerts = \@a;
  142.         }
  143.         foreach my $a (@{$alerts}) {
  144.             $a->{service_description} = "" if ref $a->{service_description} eq 'HASH';
  145.             my $output = $a->{output};
  146.             utf8::decode($output);
  147.             $a->{output} = $output;
  148.         }
  149.     }
  150.     $report->{alerts} = $alerts;
  151.     return  $report;
  152. }
  153.  
  154. sub centreon_step($) {
  155.     my $report = shift;
  156.     my $sid = get_centreon_session($report->{centreon_user},
  157.             $report->{centreon_pass});
  158.  
  159. # Set groupids
  160.     my $h;
  161.     foreach my $key (keys  %{$report->{apps}}) {
  162.         my $app =  $report->{apps}->{$key};
  163.         my $group = $app->{centreon_group};
  164.         $h->{$group}->{id} = get_group_id($group,
  165.                 $report->{centreon_user},
  166.                 $report->{centreon_pass},);
  167.     }
  168.     foreach my $g (@{$report->{centreon_extra_groups}}) {
  169.         $h->{$g}->{id} = get_group_id($g,
  170.                 $report->{centreon_user},
  171.                 $report->{centreon_pass},);
  172.     }
  173.     $report->{groupids} = $h;
  174.  
  175. # Get reports
  176.     foreach my $key (keys  %{$report->{groupids}}) {
  177.         my $group = $report->{groupids}->{$key};
  178.         $group->{report} = get_centreon_report(
  179.                 $group->{id}, $sid);
  180.     }
  181.  
  182. # Get checks
  183.     foreach my $m (keys %{$report->{machines}}) {
  184.         my $h = $report->{machines}->{$m};
  185.         $h->{checks} = get_centreon_checks(
  186.                 $h->{hostname}, $sid);
  187.     }
  188.     return $report;
  189. }
  190. 1;
  191.  
  192. #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&notification=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'
  193. =begin
  194. Hostgroup;Begin date; End date; Duration
  195. cutm_culture; 01/06/2015 00:00:00; 30/06/2015 00:00:00; 2505600s
  196.  
  197. Status;Total Time;Mean Time; Alert
  198. DOWN;0;0%;0;
  199. UP;100;100%;1;
  200. UNREACHABLE;0;0%;0;
  201. UNDETERMINED;0;
  202.  
  203.  
  204. 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;
  205. cu-prod-front1.cutm.nfrance.com;100;100;1;0;0;0;0;0;0;0;
  206.  
  207. =cut
  208.  
  209.  
  210.  
  211.  
  212. # http://nagios1.nfrance.com/centreon/include/reporting/dashboard/csvExport/csv_HostGroupLogs.php?sid=gpfiht3gr7ishobvq5olv0h396&hostgroup=231&start=1433109600&end=1434492000
  213. =begin
  214.  
  215. 'svc_id' => '113',
  216.     'hnl' => 'tm-prod-web1.cutm.nfrance.com',
  217.     'sc' => '#13EB3A',
  218.     'pa' => '0',
  219.     'hci' => '0',
  220.     'last_hard_state_change' => '1M 2d 16h 4m 58s',
  221.     'class' => 'list_one',
  222.     'eh' => '1',
  223.     'sdl' => 'ssh',
  224.     'hpe' => '0',
  225.     'hc' => 'transparent',
  226.     'hae' => '1',
  227.     'dtm' => '0',
  228.     'nc' => '17/06/2015 13:38:11',
  229.     'ne' => '1',
  230.     'sau' => 'none',
  231.     'hid' => '1520',
  232.     'ackXsl' => './include/monitoring/acknowlegement/xsl/popupForAck.xsl',
  233.     'ca' => '1/3 (H)',
  234.     'po' => 'SSH OK - OpenSSH_6.6.1 (protocol 2.0)',
  235.     'd' => '1M 2d 16h 4m 58s',
  236.     'sico' => {},
  237.     'sn' => 'none',
  238.     'pc' => '0',
  239.     'hs' => '0',
  240.     'dtmXsl' => './include/monitoring/downtime/xsl/popupForDowntime.xsl',
  241.     'is' => '0',
  242.     'fd' => '1',
  243.     'cs' => 'OK',
  244.     'ppd' => '1',
  245.     'ackXml' => './include/monitoring/acknowlegement/xml/broker/makeXMLForAck.php?sid=u4lhn1edd3t6obdib2j0p5d0o5&hid=1520&svc_id=113',
  246.     'o' => '7',
  247.     'lc' => '17/06/2015 13:23:11',
  248.     'snn' => 'none',
  249.     'sd' => 'ssh',
  250.     'ha' => '0',
  251.     'rd' => '2860441',
  252.     'hn' => {
  253.         'none' => '1',
  254.         'content' => 'tm-prod-web1.cutm.nfrance.com'
  255.     },
  256.     'dtmXml' => './include/monitoring/downtime/xml/broker/makeXMLForDowntime.php?sid=u4lhn1edd3t6obdib2j0p5d0o5&hid=1520&svc_id=113',
  257.     'snu' => 'none',
  258.     'svc_index' => '29007',
  259.     'ac' => '1'
  260.     =cut
  261.  
  262.  
  263. #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
  264.  
  265. #curl -i 'http://nagios1.nfrance.com/centreon/include/monitoring/status/Common/updateContactParamHostGroups.php?uid=26&hostgroups=305,306' -H 'Cookie: PHPSESSID=go18dsd7m65dq9i5ulfvn73c14'
  266.  
  267. # 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