#!/usr/bin/perl
use strict;
use warnings;

my $err=0;
my $out="";
my $ok_counter=0;
my $fail_counter=0;

open NETNS, 'ip netns | grep qrouter | sed "s/^qrouter-//g" | awk \'{ print $1 }\' |' or die($!);

sub count_process($) {
    my $string = shift;
    open PGREP, "pgrep -a -f '".$string."' | grep -v pgrep | wc -l |";
    my $count = <PGREP>;
    close PGREP;
    return $count;
}
while ( <NETNS> ) {
    my $router_uuid = $_;
    chomp($router_uuid);
    my $keepalived_count = 0;
    my $state_change_count = 0;
    $keepalived_count = count_process("keepalived.*".$router_uuid."/keepalived.conf");
    $state_change_count = count_process("neutron-keepalived-state-change.*".$router_uuid);
    if($keepalived_count < 2 || $state_change_count < 1) {
        $err = 2;
        $out .= "$router_uuid FAIL! ";
        $fail_counter++;
    } else {
        $ok_counter++;
    }
}
close (NETNS);

if($err < 1) {
    $out .= "OK - $ok_counter vRouter RUNNING "
}
print $out."\n";
exit $err;

