05653eb99a13819d61e7d56ff935405f2447abdd
[safeq] / xwc-jobs.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Data::Dump qw(dump);
6
7 my $ip = shift @ARGV || '10.60.3.35';
8 my $debug = $ENV{DEBUG} || 0;
9 my $sep = $ENV{SEP} || "\t";
10
11 my $op = shift @ARGV || 'list';
12
13 my $url;
14 my $var_re;
15
16 if ( $op =~ m/^l/i ) { # list
17         $url = 'jblist.htm';
18         $var_re = '(stats|types|info|hdrs)';
19 } elsif ( $op =~ m/^h/i ) { # history
20         $url = 'jbhist.htm';
21         $var_re = '(hdrs|stsAry|types|jHst)';
22 } elsif ( $op =~ m/^s/i ) { # status
23         $url = 'stgen.htm';
24         $var_re = '(lbls|spcs|adrslbl)';
25 } elsif ( $op =~ m/^e/i ) { # error
26         $url = 'sperr.htm';
27         $var_re = '(lHdr|errLog)';
28 } elsif ( $op =~ m/^(d|c)/i ) { # delete/cancel
29         my $job_id = join('/', @ARGV) || die "expected job_id(s) missing";
30         open(my $curl, '-|', "curl --silent -XPOST -d OPR=CANCEL -d JOBS=$job_id/ http://$ip/JOBCTRL.cmd");
31         while (<$curl>) {
32                 if ( m/<title>/i ) {
33                         chomp;
34                         s/<[^>]*>//g;
35                         print join($sep, $ip, $job_id, 'CANCEL', $_),"\n";
36                 }
37         }
38         exit 0;
39 } else {
40         die "UNKNOWN op [$op]\n";
41 }
42
43 warn "# $ip/$url" if $debug;
44 open(my $curl, '-|', "curl --silent http://$ip/$url");
45 my $info;
46 while(<$curl>) {
47         if ( m/var ${var_re}=(.*);/ ) {
48                 my $name = $1;
49                 my $json = $2;
50                 my $v = eval $json; # this is not valid JSON, but perl's eval doesn't mind
51                 warn "## JSON $name $json -> ",dump($v) if $debug > 1;
52                 $info->{$1} = $v;
53         }
54 }
55
56 warn "# info=",dump($info) if $debug;
57
58 if ( exists $info->{spcs} ) {
59         print join($sep, @{ $info->{lbls} }),"\n";
60
61         my @s = @{ $info->{spcs} };
62         foreach my $i ( 0 .. $#{ $s[1] } ) {
63                 $s[1]->[$i] .= ' ' . $info->{adrslbl}->[$i];
64         }
65         $s[1] = join(',', @{ $s[1] });
66         
67         print join($sep, @s),"\n";
68
69         exit 0;
70
71 } elsif ( exists $info->{errLog} ) {
72         print join($sep, @{ $info->{lHdr} }),"\n";
73         foreach my $error ( @{ $info->{errLog} } ) {
74                 print join($sep, @{ $error }),"\n";
75         }
76         exit 0;
77
78 }
79
80 my @headers = @{ $info->{hdrs} };
81 unshift @headers, 'id' if $op eq 'list';
82 unshift @headers, 'IP';
83
84 print join($sep, @headers),"\n";
85
86 foreach my $l ( @{ $info->{info} } ) {
87         warn "## l=",dump($l) if $debug > 1;
88         
89         $l->[3] .= ':' . $info->{stats}->[ $l->[3] ];
90         $l->[4] .= ':' . $info->{types}->[ $l->[4] ];
91
92         print join($sep, $ip, @$l), "\n";
93 }
94
95 foreach my $l ( @{ $info->{jHst} } ) {
96         warn "## l=",dump($l) if $debug > 1;
97         
98         $l->[2] .= ':' . $info->{stsAry}->[ $l->[2] ];
99         $l->[3] .= ':' . $info->{types}->[ $l->[3] ];
100
101         print join($sep, $ip, @$l),"\n";
102 }