File Coverage

lib/Ubic/Ping.pm
Criterion Covered Total %
statement 21 81 25.9
branch 0 24 0.0
condition n/a
subroutine 7 11 63.6
pod 1 1 100.0
total 29 117 24.7


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             package Ubic::Ping;
3             $Ubic::Ping::VERSION = '1.60';
4 1     1   675 use strict;
  1         1  
  1         23  
5 1     1   3 use warnings;
  1         1  
  1         19  
6              
7             # ABSTRACT: http server which returns service status by it's name or port
8              
9 1     1   3 use Ubic;
  1         0  
  1         17  
10 1     1   3 use Ubic::PortMap;
  1         1  
  1         23  
11 1     1   3 use Params::Validate qw(:all);
  1         2  
  1         121  
12 1     1   3 use Try::Tiny;
  1         3  
  1         41  
13              
14 1     1   3 use parent qw(HTTP::Server::Simple::CGI);
  1         1  
  1         4  
15              
16             sub _print_status($;$) {
17 0     0     my ($name, $options) = validate_pos(@_, 1, { type => HASHREF, default => {} });
18              
19 0 0         unless (Ubic->is_enabled($name)) {
20 0 0         if ($options->{noc}) {
21 0           print "HTTP/1.1 500 Disabled\r\n";
22             }
23             else {
24 0           print "HTTP/1.1 200 OK\r\n";
25             }
26 0           print "Content-Type: text/plain; charset=utf-8\r\n\r\n";
27 0           print "disabled\n";
28 0           return;
29             }
30 0           my $status = Ubic->cached_status($name)->status; # should read status from static file on disk
31 0 0         if ($status eq 'running') {
32 0           print "HTTP/1.1 200 OK\r\n";
33 0           print "Content-Type: text/plain; charset=utf-8\r\n\r\n";
34 0           print "ok\n";
35 0           return;
36             }
37             else {
38 0 0         if ($options->{noc}) {
39 0 0         if ($status =~ /^[\w ]+$/) {
40 0           print "HTTP/1.1 500 $status\r\n";
41             }
42             else {
43             # invalid status, fallback to default status message
44 0           print "HTTP/1.1 500 Wrong status\r\n";
45             }
46             }
47             else {
48 0           print "HTTP/1.1 200 OK\r\n";
49             }
50 0           print "Content-Type: text/plain; charset=utf-8\r\n\r\n";
51 0           print "$status\n";
52 0           return;
53             }
54             }
55              
56             sub handle_request {
57 0     0 1   my ($self, $cgi) = @_;
58              
59             try {
60 0 0   0     if ($cgi->path_info eq '/ping') {
    0          
    0          
    0          
61             # ping self
62 0           print "HTTP/1.1 200 OK\r\n";
63 0           print "Content-Type: text/plain; charset=utf-8\r\n\r\n";
64 0           print "ok\n";
65 0           return;
66             }
67             elsif ( $cgi->path_info =~ m{^/noc/(\d+)/?$}) {
68 0           my $port = $1;
69 0           my $name = Ubic::PortMap::port2name($port);
70 0 0         unless (defined $name) {
71 0           print "HTTP/1.1 404 Not found\r\n";
72 0           print "Content-Type: text/plain; charset=utf-8\r\n\r\n";
73 0           print "Service at port '$port' not found\n";
74 0           return;
75             }
76 0           _print_status($name, { noc => 1 });
77 0           return;
78             }
79             elsif ($cgi->path_info =~ m{^/status/port/(\d+)/?$}) {
80 0           my $port = $1;
81 0           my $name = Ubic::PortMap::port2name($port);
82 0 0         unless (defined $name) {
83 0           print "HTTP/1.1 404 Not found\r\n";
84 0           print "Content-Type: text/plain; charset=utf-8\r\n\r\n";
85 0           print "Service at port '$port' not found\n";
86 0           return;
87             }
88 0           _print_status($name);
89 0           return;
90             }
91             elsif (my ($name) = $cgi->path_info =~ m{^/status/service/(.+?)/?$}) {
92 0 0         unless (Ubic->has_service($name)) {
93 0           print "HTTP/1.1 404 Not found\r\n";
94 0           print "Content-Type: text/plain; charset=utf-8\r\n\r\n";
95 0           print "Service with name '$name' not found\n";
96 0           return;
97             }
98 0           _print_status($name);
99             }
100             else {
101 0           print "HTTP/1.1 404 Not found\r\n";
102 0           print "Content-Type: text/plain; charset=utf-8\r\n\r\n";
103 0           print "Expected /status/service/NAME or /status/port/PORT query\n";
104 0           return;
105             }
106             }
107             catch {
108 0     0     print "HTTP/1.1 500 Internal error\r\n";
109 0           print "Content-Type: text/plain; charset=utf-8\r\n\r\n";
110 0           print "Error: $_";
111 0           };
112             }
113              
114             1;
115              
116             __END__