Index: /trunk/tools/czartool.pl
===================================================================
--- /trunk/tools/czartool.pl	(revision 27774)
+++ /trunk/tools/czartool.pl	(revision 27775)
@@ -11,64 +11,192 @@
 use File::Temp qw(tempfile);
 
-my $dbname = 'gpc1';
-my $dbserver = 'ippdb01';
-my $dbuser = 'ippuser';
-my $dbpass = 'ippuser';
-my $db = DBI->connect(
-        "DBI:mysql:database=${dbname};host=${dbserver};mysql_socket=" .
-        DB_SOCKET(),${dbuser},${dbpass},
-        {
-        RaiseError => 1, AutoCommit => 1}
-        ) or die "Unable to connect to database $DBI::errstr\n";
-
-# get current labels from pantasks
-my @cmdOut = `echo "show.labels;quit" | pantasks_client -c ~ipp/stdscience/ptolemy.rc`;
-my $line;
-my @labels;
-my $passedHeader=0;
-foreach $line (@cmdOut) {
-
-    chomp($line);
-    if ($line =~ m/pantasks: dummy/) {$passedHeader=1; next;}
-
-    if ($passedHeader){push(@labels, $line);}
-}
-
-# define set of states
 my @states = ("full", "new", "drop", "wait", "fault");
+my $db = connectToDb();
+
+if (!$db) {die;}
+
+my @stdscienceLabels = loadLabels("stdscience");
+my @distributionLabels = loadLabels("distribution");
+showLabelDifferences();
 
 checkAllLabels("new");
 printInstructions();
 
-my $key;
-system "stty cbreak < /dev/tty > /dev/tty 2>&1";
-while (($key=getc) ) {
-    last if $key eq "q";
-
-    if ($key eq "f") {checkAllLabels("full");}
-    elsif ($key eq "n") {checkAllLabels("new");}
-    elsif ($key eq "d") {checkAllLabels("drop");}
-    elsif ($key eq "w") {checkAllLabels("wait");}
-    elsif ($key eq "l") {labelDetails();}
-    elsif ($key =~ m/[0-9]/) {my $key2=getc; checkOneLabel($labels[$key.$key2]);}
-    printInstructions();
-
-};
-system "stty -cbreak < /dev/tty > /dev/tty 2>&1";
+poll();
 $db->disconnect();
 
-##############
-#
-#
-###############
+###########################################################################
+#
+# Prints simple instructions
+#
+###########################################################################
 sub printInstructions {
 
-    print "\n*** Enter: (f)ull (n)ew (d)rop (w)ait (q)uit (l)abel, or label number from table above: ";
-}
-
-##############
-#
-#
-###############
+    print "\n### Enter: (s)ervers (f)ull (n)ew (d)rop (w)ait (q)uit (l)abel, or label number from table above: ";
+}
+
+###########################################################################
+#
+# Polls waiting for input from user 
+#
+###########################################################################
+sub poll {
+
+    my $key;
+    system "stty cbreak < /dev/tty > /dev/tty 2>&1";
+    while (($key=getc) ) {
+        last if $key eq "q";
+
+        if ($key eq "s") {checkServers();}
+        elsif ($key eq "f") {checkAllLabels("full");}
+        elsif ($key eq "n") {checkAllLabels("new");}
+        elsif ($key eq "d") {checkAllLabels("drop");}
+        elsif ($key eq "w") {checkAllLabels("wait");}
+        elsif ($key eq "l") {labelDetails();}
+        elsif ($key =~ m/[0-9]/) {my $key2=getc; checkOneLabel($stdscienceLabels[$key.$key2]);}
+        printInstructions();
+
+    };
+    system "stty -cbreak < /dev/tty > /dev/tty 2>&1";
+}
+
+###########################################################################
+#
+# Connects to the database
+#
+###########################################################################
+sub connectToDb {
+
+    my $dbname = 'gpc1';
+    my $dbserver = 'ippdb01';
+    my $dbuser = 'ippuser';
+    my $dbpass = 'ippuser';
+    my $db = DBI->connect(
+            "DBI:mysql:database=${dbname};host=${dbserver};mysql_socket=" .
+            DB_SOCKET(),${dbuser},${dbpass},
+            {
+            RaiseError => 1, AutoCommit => 1}
+            ) or die "Unable to connect to database $DBI::errstr\n";
+
+    return $db;
+}
+
+###########################################################################
+#
+# Display label differences between stdscience and distribution
+#
+###########################################################################
+sub showLabelDifferences {
+
+    my @diffLabels = @{getArrayDifferences(\@stdscienceLabels, \@distributionLabels)};
+
+    printf("+-------------------------------------------------------+\n");
+    printf("| Label differences between stdscience and distribution |\n");
+    printf("+-------------------------------------------------------+\n");
+
+    my $item;
+    foreach $item (@diffLabels) { 
+
+        printf( "| %53s |\n", $item);
+    }
+    printf("+-------------------------------------------------------+\n");
+}
+
+###########################################################################
+#
+# Compares two arrays and stores the differences
+#
+###########################################################################
+sub getArrayDifferences {
+
+    my @array1 = @{$_[0]};
+    my @array2 = @{$_[1]};
+
+    my @isect;
+    my @diff;
+    my %count;
+
+    my $item;
+    foreach $item (@array1, @array2) { $count{$item}++;}
+
+    foreach $item (keys %count) {
+        if ($count{$item} == 2) {
+            push @isect, $item;
+        } else {
+            push @diff, $item;
+        }
+    }
+
+    return \@diff;
+}
+
+###########################################################################
+#
+# Loads labels from a particular pantasks server
+#
+###########################################################################
+sub loadLabels {
+    my ($server) = @_;
+
+    my @cmdOut = `echo "show.labels;quit" | pantasks_client -c ~ipp/$server/ptolemy.rc 2>&1`;
+    my $line;
+    my @labels;
+    my $passedHeader=0;
+    foreach $line (@cmdOut) {
+
+        chomp($line);
+        if ($line =~ m/pantasks:/) {$passedHeader=1; next;}
+
+        if ($passedHeader){push(@labels, $line);}
+    }
+
+    return @labels;
+}
+
+###########################################################################
+#
+# Checks all servers to see if they are alive and running
+#
+###########################################################################
+sub checkServers {
+
+    my @servers = ("addstar", "cleanup", "detrend", "distribution", "pstamp", "publishing", "registration", "replication", "stdscience", "summitcopy");
+    printf("\n+-----------------------------------------------+\n");
+    printf("|                      Servers                  |\n");
+    printf("+--------------+---------+----------------------+\n");
+    printf("|    server    |  alive? |  Scheduler running?  |\n");
+    printf("+--------------+---------+----------------------+\n");
+    my $server;
+    foreach $server (@servers) {
+
+        printf("| %12s ", $server);
+
+        my @cmdOut = `echo "status ;quit" | pantasks_client -c ~ipp/$server/ptolemy.rc 2>&1`;
+
+        my $line;
+        my $foundStatus = 0;
+        my $isRunning = 0;
+        foreach $line (@cmdOut) {
+
+            chomp($line);
+            if ($line =~ m/Scheduler is stopped/) {$isRunning = 0; $foundStatus=1;last;}
+            if ($line =~ m/Scheduler is running/) {$isRunning = 1; $foundStatus=1;last;}
+            if ($line =~ m/Task Staus/) {last;}
+
+        }
+
+        if (!$foundStatus){print "|    NO   ";} else {print "|   yes   ";}
+        if (!$isRunning){print "|           NO         ";} else {print "|          yes         ";}
+        printf("|\n");
+    }
+
+    printf("+--------------+---------+----------------------+\n");
+}
+
+###########################################################################
+#
+# Takes label name or number
+#
+###########################################################################
 sub labelDetails {
 
@@ -77,5 +205,5 @@
     chomp($input);
     if ($input =~ m/^([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$/) {
-       return checkOneLabel($labels[$input]);
+       return checkOneLabel($stdscienceLabels[$input]);
     }
 
@@ -83,8 +211,9 @@
 }
 
-##############
-#
-#
-###############
+###########################################################################
+#
+# Checks one label and prints results in a table
+#
+###########################################################################
 sub checkOneLabel {
     my ($label) = @_;
@@ -121,8 +250,10 @@
     close(LOGFILE);
 }
-##############
-#
-#
-###############
+
+###########################################################################
+#
+# Checks all labels for a particular state and prints results in a table
+#
+###########################################################################
 sub checkAllLabels {
     my ($state) = @_;
@@ -136,5 +267,5 @@
     my $label;
     my $i=0;
-    foreach $label (@labels) {
+    foreach $label (@stdscienceLabels) {
 
         chomp($label);
@@ -159,8 +290,9 @@
 }
 
-##############
-#
-#
-###############
+###########################################################################
+#
+# Returns state and fault-count (if new) as a string
+#
+###########################################################################
 sub getStateAndFaults {
     my ($label, $table, $state, $stage) = @_;
@@ -177,8 +309,9 @@
 }
 
-##############
-#
-#
-###############
+###########################################################################
+#
+# Returns count of exposures with this state for this label
+#
+###########################################################################
 sub checkLabel {
     my ($label, $table, $state, $stage) = @_;
@@ -198,9 +331,9 @@
 }
 
-
-##############
-#
-#
-###############
+###########################################################################
+#
+# Returns count of faults for this stage and label
+#
+###########################################################################
 sub countFaults {
     my ($label, $table, $stage) = @_;
@@ -236,8 +369,9 @@
 }
 
-##############
-#
-#
-###############
+###########################################################################
+#
+# Counts total distribution faults
+#
+###########################################################################
 sub countDistFaults {
     my ($label) = @_;
