added s[tatus] to scrape stgen.htm
[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/^(d|c)/i ) { # delete/cancel
26         my $job_id = join('/', @ARGV) || die "expected job_id(s) missing";
27         open(my $curl, '-|', "curl --silent -XPOST -d OPR=CANCEL -d JOBS=$job_id/ http://$ip/JOBCTRL.cmd");
28         while (<$curl>) {
29                 if ( m/<title>/i ) {
30                         chomp;
31                         s/<[^>]*>//g;
32                         print join($sep, $ip, $job_id, 'CANCEL', $_),"\n";
33                 }
34         }
35         exit 0;
36 } else {
37         die "UNKNOWN op [$op]\n";
38 }
39
40 warn "# $ip/$url" if $debug;
41 open(my $curl, '-|', "curl --silent http://$ip/$url");
42 my $info;
43 while(<$curl>) {
44         if ( m/var ${var_re}=(.*);/ ) {
45                 my $name = $1;
46                 my $json = $2;
47                 my $v = eval $json; # this is not valid JSON, but perl's eval doesn't mind
48                 warn "## JSON $name $json -> ",dump($v) if $debug > 1;
49                 $info->{$1} = $v;
50         }
51 }
52
53 warn "# info=",dump($info) if $debug;
54
55 if ( exists $info->{spcs} ) {
56         print join($sep, @{ $info->{lbls} }),"\n";
57
58         my @s = @{ $info->{spcs} };
59         foreach my $i ( 0 .. $#{ $s[1] } ) {
60                 $s[1]->[$i] .= ' ' . $info->{adrslbl}->[$i];
61         }
62         $s[1] = join(',', @{ $s[1] });
63         
64         print join($sep, @s),"\n";
65
66         exit 0;
67 }
68
69 my @headers = @{ $info->{hdrs} };
70 unshift @headers, 'id' if $op eq 'list';
71 unshift @headers, 'IP';
72
73 print join($sep, @headers),"\n";
74
75 foreach my $l ( @{ $info->{info} } ) {
76         warn "## l=",dump($l) if $debug > 1;
77         
78         $l->[3] .= ':' . $info->{stats}->[ $l->[3] ];
79         $l->[4] .= ':' . $info->{types}->[ $l->[4] ];
80
81         print join($sep, $ip, @$l), "\n";
82 }
83
84 foreach my $l ( @{ $info->{jHst} } ) {
85         warn "## l=",dump($l) if $debug > 1;
86         
87         $l->[2] .= ':' . $info->{stsAry}->[ $l->[2] ];
88         $l->[3] .= ':' . $info->{types}->[ $l->[3] ];
89
90         print join($sep, $ip, @$l),"\n";
91 }