root/lib/Resmon/Status.pm

Revision ae5283c7e99faf2b995945c770a82f65747b3616, 7.1 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

add configuration to the XML dump

git-svn-id: https://labs.omniti.com/resmon/trunk@33 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

  • Property mode set to 100644
Line 
1 package Resmon::Status;
2
3 use strict;
4 use POSIX qw/:sys_wait_h/;
5 use IO::Handle;
6 use IO::File;
7 use IO::Socket;
8 use Socket;
9 use Fcntl qw/:flock/;
10 use IPC::SysV qw /IPC_CREAT IPC_RMID ftok S_IRWXU S_IRWXG S_IRWXO/;
11 use Data::Dumper;
12
13 my $SEGSIZE = 1024*256;
14 my $statusfile;
15 sub new {
16   my $class = shift;
17   my $file = shift;
18   return $statusfile if($statusfile);
19   return bless {
20     file => $file
21   }, $class;
22 }
23 sub get_shared_state {
24   my $self = shift;
25   my $blob;
26   my $len;
27   return unless($self->{shared_state});
28   # Lock shared segment
29   # Read in
30   shmread($self->{shared_state}, $len, 0, length(pack('i', 0)));
31   $len = unpack('i', $len);
32   shmread($self->{shared_state}, $blob, length(pack('i', 0)), $len);
33   # unlock
34   my $VAR1;
35   eval $blob;
36   die $@ if ($@);
37   $self->{store} = $VAR1;
38   return $self->{store};
39 }
40 sub store_shared_state {
41   my $self = shift;
42   return unless($self->{shared_state});
43   my $blob = Dumper($self->{store});
44
45   # Lock shared segment
46   # Write state and flush
47   shmwrite($self->{shared_state}, pack('i', length($blob)),
48            0, length(pack('i', 0))) || die "$!";
49   shmwrite($self->{shared_state}, $blob, length(pack('i', 0)),
50            length($blob)) || die "$!";
51   # unlock
52 }
53 sub xml_kv_dump {
54   my $info = shift;
55   my $indent = shift || 0;
56   my $rv = '';
57   while(my ($key, $value) = each %$info) {
58     $rv .= " " x $indent;
59     if(ref $value eq 'HASH') {
60       $rv .= "<$key>\n";
61       $rv .= xml_kv_dump($value, $indent + 2);
62       $rv .= " " x $indent;
63       $rv .= "</$key>\n";
64     }
65     else {
66       $rv .= "<$key>$value</$key>\n";
67     }
68   }
69   return $rv;
70 }
71 sub xml_info {
72   my ($module, $service, $info) = @_;
73   my $rv = '';
74   $rv .= "  <ResmonResult module=\"$module\" service=\"$service\">\n";
75   $rv .= xml_kv_dump($info, 4);
76   $rv .= "  </ResmonResult>\n";
77   return $rv;
78 }
79 sub dump_generic {
80   my $self = shift;
81   my $dumper = shift;
82   my $rv = '';
83   while(my ($module, $services) = each %{$self->{store}}) {
84     while(my ($service, $info) = each %$services) {
85       $rv .= $dumper->($module,$service,$info);
86     }
87   }
88   return $rv;
89 }
90 sub dump_oldstyle {
91   my $self = shift;
92   my $response = $self->dump_generic(sub {
93     my($module,$service,$info) = @_;
94     return "$service($module) :: $info->{state}($info->{message})\n";
95   });
96   return $response;
97 }
98 sub dump_xml {
99   my $self = shift;
100   my $response = <<EOF
101 <?xml version="1.0" encoding="UTF-8"?>
102 <ResmonResults>
103 EOF
104   ;
105   $response .= $self->dump_generic(\&xml_info);
106   $response .= "</ResmonResults>\n";
107   return $response;
108 }
109 sub service {
110   my $self = shift;
111   my ($client, $req, $proto) = @_;
112   my $state = $self->get_shared_state();
113   if($req eq '/' or $req eq '/status') {
114     my $response .= $self->dump_xml();
115     $client->print(http_header(200, $proto?length($response):0));
116     $client->print($response . "\r\n");
117     return;
118   } elsif($req eq '/status.txt') {
119     my $response = $self->dump_oldstyle();
120     $client->print(http_header(200, $proto?length($response):0, 'text/plain'));
121     $client->print($response . "\r\n");
122     return;
123   } else {
124     if($req =~ /^\/([^\/]+)\/(.+)$/) {
125       if(exists($self->{store}->{$1}) &&
126          exists($self->{store}->{$1}->{$2})) {
127         my $info = $self->{store}->{$1}->{$2};
128         my $response = qq^<?xml version="1.0" encoding="UTF-8"?>\n^;
129         $response .= "<ResmonResults>\n".
130                      xml_info($1,$2,$info).
131                      "</ResmonRestults>\n";
132         $client->print(http_header(200, $proto?length($response):0));
133         $client->print( $response . "\r\n");
134         return;
135       }
136     }
137   }
138   die "Request not understood\n";
139 }
140 sub http_header {
141   my $code = shift;
142   my $len = shift;
143   my $type = shift || 'text/xml';
144   return qq^HTTP/1.0 $code OK
145 Server: resmon
146 ^ . (defined($len) ? "Content-length: $len" : "Connection: close") . q^
147 Content-Type: text/plain; charset=utf-8
148
149 ^;
150 }
151 sub serve_http_on {
152   my $self = shift;
153   my $ip = shift;
154   my $port = shift;
155   $ip = INADDR_ANY if(!defined($ip) || $ip eq '' || $ip eq '*');
156   $port ||= 81;
157
158   my $handle = IO::Socket->new();
159   socket($handle, PF_INET, SOCK_STREAM, getprotobyname('tcp'))
160     || die "socket: $!";
161   setsockopt($handle, SOL_SOCKET, SO_REUSEADDR, pack("l", 1))
162     || die "setsockopt: $!";
163   bind($handle, sockaddr_in($port, $ip))
164     || die "bind: $!";
165   listen($handle,SOMAXCONN);
166
167   $self->{http_port} = $port;
168   $self->{http_ip} = $ip;
169
170   $self->{child} = fork();
171   if($self->{child} == 0) {
172     eval {
173       while(my $client = $handle->accept) {
174         my $req;
175         my $proto;
176         while(<$client>) {
177           eval {
178             s/\r\n/\n/g;
179             chomp;
180             if(!$req) {
181               if(/^GET \s*(\S+)\s*?(?: HTTP\/(0\.9|1\.0|1\.1)\s*)?$/) {
182                 $req = $1;
183                 $proto = $2;
184               }
185               else {
186                 die "protocol deviations.\n";
187               }
188             }
189             elsif(/^$/) {
190               $self->service($client, $req, $proto);
191               last unless ($proto);
192               $req = undef;
193               $proto = undef;
194             }
195             elsif(/^\S+\s*:\s*.{1,4096}$/) {
196               # Valid request header... noop
197             }
198             else {
199               die "protocol deviations.\n";
200             }
201           };
202           if($@) {
203             print $client http_header(500, 0, 'text/plain');
204             print $client "$@\r\n";
205             last;
206           }
207         }
208         $client->close();
209       }
210     };
211     if($@) {
212       print STDERR "Error in listener: $@\n";
213     }
214     exit(0);
215   }
216   close($handle);
217   return;
218 }
219 sub open {
220   my $self = shift;
221   return 0 unless(ref $self);
222   return 1 if($self->{handle});  # Alread open
223   if($self->{file} eq '-' || !defined($self->{file})) {
224     $self->{handle} = IO::File->new_from_fd(fileno(STDOUT), "w");
225     return 1;
226   }
227   $self->{handle} = IO::File->new("> $self->{file}.swap");
228   die "open $self->{file}.swap failed: $!\n" unless($self->{handle});
229   $self->{swap_on_close} = 1; # move this to a non .swap version on close
230   chmod 0644, "$statusfile.swap";
231
232   unless($self->{shared_state}) {
233     my $id = ftok(__FILE__,$self->{http_port});
234     $self->{shared_state} = shmget($id, $SEGSIZE,
235                                    IPC_CREAT|S_IRWXU|S_IRWXG|S_IRWXO)
236       || die "$0: $!";
237   }
238   return 1;
239 }
240 sub store {
241   my ($self, $type, $name, $info) = @_;
242   %{$self->{store}->{$type}->{$name}} = %$info;
243   $self->{store}->{$type}->{$name}->{last_update} = time;
244   $self->store_shared_state();
245   if($self->{handle}) {
246     $self->{handle}->print("$name($type) :: $info->{state}($info->{message})\n");
247   } else {
248     print "$name($type) :: $info->{state}($info->{message})\n";
249   }
250 }
251 sub close {
252   my $self = shift;
253   $self->{handle}->close() if($self->{handle});
254   $self->{handle} = undef;
255   if($self->{swap_on_close}) {
256     unlink("$self->{file}");
257     link("$self->{file}.swap", $self->{file});
258     unlink("$self->{file}.swap");
259     delete($self->{swap_on_close});
260   }
261 }
262 sub DESTROY {
263   my $self = shift;
264   my $child = $self->{child};
265   if($child) {
266     kill 15, $child;
267     sleep 1;
268     kill 9, $child if(kill 0, $child);
269     waitpid(-1,WNOHANG);
270   }
271   if($self->{shared_state}) {
272     shmctl($self->{shared_state}, IPC_RMID, 0);
273   }
274 }
275 1;
Note: See TracBrowser for help on using the browser.