blib/lib/Ubic/Ping.pm untested

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