Video Screencast Help
Symantec to Separate Into Two Focused, Industry-Leading Technology Companies. Learn more.

Custom Script: VCS uptime script

Created: 17 Nov 2010 • Updated: 25 Nov 2010
Seann Herdejurgen's picture
+2 2 Votes
Login to vote

The following Perl script displays the uptime of a VCS cluster.  Perhaps the most interesting part of the script is the section of code that obtains output of the haclus -display, hasys -display and hagrp -display commands and inserts it into a hash called %status.  The rest of the script simply pulls data from the %status hash to determine the output.

Best Regards,

-Seann

#!/usr/bin/perl
#
#  vcsuptime
#  Written by Seann Herdejurgen
#
#  This script displays the uptime of a VCS cluster.
#
#     > vcsuptime
#     Cluster Name  - oraclus  up 368 day(s), 5:42
#     Group Status  - Online: 49 Offline: 29 Partial: 1
#     System Status - Online: 6
#     Link Status   - Up
#

use strict;

if ( ! -d "/opt/VRTSvcs/bin" ) {
   print STDERR "VCS is not installed on this system\n";
   exit 1;
}

my (%status,%syscount,%groups,%grpcount);

my %sysstatus=(
   RUNNING=>"Online",
      EXIT=>"Exited",
   FAULTED=>"Faulted",
   UNKNOWN=>"Unknown"
);

my %grpstatus=(
    ONLINE=>"Online",
   OFFLINE=>"Offline",
   PARTIAL=>"Partial",
   FAULTED=>"Faulted"
);

# Get VCS status
for my $command (qw/haclus hasys hagrp/) {
   my $fields=0;
   foreach my $line (split /\n/,`/opt/VRTSvcs/bin/$command -display`) {
      next if ($line =~ /^#$/);
      if ($line =~ /^#/) { $fields=split /\s+/,$line; next; }
      my (@fields)=split /\s+/,$line,$fields;
      my $value=pop @fields;
      my $key=pop @fields;
      my $href=\%status;
      foreach my $field ($command,@fields) {
         $href->{$field}||={};
         $href=$href->{$field};
      }
      $href->{$key}=$value;
   }
}

# Calculate VCS uptime
my $uptime=$status{haclus}{GlobalCounter}*$status{haclus}{CounterInterval}/60;
my ($min,$hrs,$days);
$min=$uptime % 60;
$uptime/=60;
$hrs=$uptime % 24;
$days=$uptime/24;
$uptime=sprintf("Cluster Name  - %s  up %d day(s), %d:%02d\n",$status{haclus}{ClusterName},$days,$hrs,$min);

# Determine group status
for my $group (keys %{$status{hagrp}}) {
   for my $attr (keys %{$status{hagrp}{$group}}) {
      next if ($attr ne "State");
      for my $system (keys %{$status{hagrp}{$group}{$attr}}) {
         for my $state (qw/ONLINE OFFLINE PARTIAL FAULTED/) {
            $groups{$state}{$group}=1 if ($status{hagrp}{$group}{$attr}{$system} =~ /$state/);
         }
      }
   }
}
$grpcount{FAULTED}=keys %{$groups{FAULTED}};
$grpcount{PARTIAL}=0;
for my $group (keys %{$groups{FAULTED}}) {
   delete $groups{ONLINE}{$group} if (exists $groups{ONLINE}{$group});
}
$grpcount{ONLINE}=keys %{$groups{ONLINE}};
for my $group (keys %{$groups{PARTIAL}}) {
   $grpcount{PARTIAL}++ if (!$groups{ONLINE}{$group} && !$groups{FAULTED}{$group});
}
$grpcount{OFFLINE}=scalar(keys %{$status{hagrp}}) - $grpcount{ONLINE} - $grpcount{PARTIAL} - $grpcount{FAULTED};
$uptime.="Group Status  -";
for my $state (qw/ONLINE OFFLINE PARTIAL FAULTED/) {
   $uptime.=" $grpstatus{$state}: $grpcount{$state}" if ($grpcount{$state});
}

# Determine system status
$uptime.="\nSystem Status -";
for my $system (keys %{$status{hasys}}) {
   $syscount{$status{hasys}{$system}{SysState}}++;
}
for my $state (qw/RUNNING EXIT FAULTED UNKNOWN/) {
   $uptime.=" $sysstatus{$state}: $syscount{$state}" if ($syscount{$state});
}

# Determine heartbeat link status
my ($hbdown,$jeopardy)=0;
for my $system (keys %{$status{hasys}}) {
   $hbdown=1   if ($status{hasys}{$system}{LinkHbStatus} =~ /DOWN/);
   $jeopardy=1 if ($status{hasys}{$system}{UpDownState} =~ /jeopardy/);
}
my $links="Up";
$links="Partial" if ($hbdown);
$links="Jeopardy" if ($jeopardy);
$uptime.="\nLink Status   - $links\n";

print $uptime;