Created
September 12, 2018 09:58
-
-
Save stasikos/d73f4a3e24748a9ff4f681ca8b7ea55a to your computer and use it in GitHub Desktop.
pacemaker cib parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use XML::Simple; | |
use Data::Dumper; | |
use JSON::RPC::Client; | |
use strict; | |
use zabbix; | |
my $cibfile='/var/lib/pacemaker/cib/cib.xml'; | |
my $zabbix_user = 'apiuser'; | |
my $zabbix_password = 'apipassword'; | |
sub parse_config { | |
my $res = shift @_; | |
my $file = shift @_; | |
print "Opening $file for $res\n"; | |
if (open (my $CONF, '<', $file)) { | |
my @sections; | |
my $scp = ''; | |
my $sch = ''; | |
my $data = 0; | |
my %section = (); | |
while (my $line = <$CONF>) { | |
chomp $line; | |
if ($line =~ /^\[DEFAULT\]/ or $line =~ /^\[SESSION\]/) { | |
if ($data > 0) { | |
push @sections, {%section}; | |
%section = (); | |
$data = 0; | |
} | |
} elsif($line =~ /^SocketConnectHost/) { | |
$sch = $line; | |
$data++; | |
$sch =~ s/.*=(.*)$/$1/; | |
$section{'SocketConnectHost'} = $sch; | |
} elsif($line =~ /^SocketConnectPort/) { | |
$data++; | |
$scp = $line; | |
$scp =~ s/.*=(.*)$/$1/; | |
$section{'SocketConnectPort'} = $scp; | |
} else { | |
# print "'$line' not match something\n"; | |
} | |
} | |
# process last session too; | |
if ($data > 0) { | |
push @sections, {%section}; | |
%section = (); | |
$data = 0; | |
} | |
print "----- Parsing result ---------------------------------\n"; | |
# print Dumper \@sections; | |
foreach my $section (@sections) { | |
if ($section->{SocketConnectHost} && $section->{SocketConnectPort} > 0) { | |
print "Session Defined\n"; | |
print "Creating host $res\n"; | |
my $client = JSON::RPC::Client->new; | |
my $auth = authenticate($client, $zabbix_user, $zabbix_password); | |
my $host_id = host_id($client, $auth, $res); | |
if (!$host_id) { | |
$host_id = host_create($client, $auth, $res); | |
} else { | |
print "Host exists\n"; | |
} | |
my $item = "connections[".$section->{SocketConnectHost}.":".$section->{SocketConnectPort}."]"; | |
my $item_desc = "Number of active connections to ".$section->{SocketConnectHost}.":".$section->{SocketConnectPort}; | |
print "Creating item $item\n"; | |
if (!item_exist($client, $auth, $host_id, $item)) { | |
item_create($client, $auth, $host_id, $item, $item_desc); | |
} else { | |
print "Item exists\n"; | |
} | |
my $trigger = "{$res:connections[".$section->{SocketConnectHost}.":".$section->{SocketConnectPort}."].max(190)}<1"; | |
my $trigger_desc = "{HOSTNAME} has no active connections to ".$section->{SocketConnectHost}.":".$section->{SocketConnectPort}; | |
print "Creating trigger $trigger\n"; | |
if (!trigger_exist($client, $auth, $host_id, $trigger, $trigger_desc)) { | |
trigger_create($client, $auth, $host_id, $trigger, $trigger_desc); | |
} else { | |
print "Trigger exists\n"; | |
} | |
} | |
} | |
close $CONF; | |
} else { | |
print "$file can't be opened, skipping\n"; | |
} | |
} | |
my $xs = XML::Simple->new; | |
my $cib = $xs->XMLin($cibfile, KeyAttr => {primitive => 'id'}); | |
my $primitives = $cib->{configuration}->{resources}->{primitive}; | |
#print Dumper $cib; | |
#print Dumper $primitives; | |
my $resource; | |
my %primitives = %$primitives; | |
for $resource (keys(%$primitives)) { | |
my $res = $primitives{$resource}; | |
my $restype = $res->{'type'}; | |
if ($resource =~ /adapt.*/ && $restype eq 'anything') { | |
my @attributes = $res->{'instance_attributes'}->{'nvpair'}; | |
# print Dumper @attributes; | |
foreach my $a (@attributes) { | |
my $cmdline = ''; | |
my $workdir = ''; | |
foreach my $attr (@{$a}) { | |
my $data = $attr; | |
my $param = $data->{'name'}; | |
if ($param eq 'cmdline_options') { | |
$cmdline = $data->{'value'}; | |
} elsif ($param eq 'workdir') { | |
$workdir = $data->{'value'}; | |
} | |
} | |
# print $restype . ">$resource in $workdir config=>" . $cmdline . "\n"; | |
parse_config($resource, $workdir . $cmdline); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment