| 1 | #!/usr/bin/perl | ||||||||||||||||||||||
| 2 | package Ubic::Ping; | ||||||||||||||||||||||
| 3 | 1 | $Ubic::Ping::VERSION = '1.59'; | |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | # ABSTRACT: http server which returns service status by it's name or port | ||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 1 | ▶ | use Ubic; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 1 | ▶ | use Ubic::PortMap; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 1 | ▶ | use Params::Validate qw(:all); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 1 | ▶ | use parent qw(HTTP::Server::Simple::CGI); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | sub _print_status($;$) { | ||||||||||||||||||||||
| 17 | 0 | ▶ | my ($name, $options) = validate_pos(@_, 1, { type => HASHREF, default => {} }); | ||||||||||||||||||||
|
Subroutine
_print_status: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | 0 | ▶ | unless (Ubic->is_enabled($name)) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 20 | 0 | ▶ | if ($options->{noc}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | ▶ | if ($status eq 'running') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | ▶ | if ($options->{noc}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 39 | 0 | ▶ | if ($status =~ /^[\w ]+$/) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | ▶ | my ($self, $cgi) = @_; | ||||||||||||||||||||
|
Subroutine
handle_request: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | try { | ||||||||||||||||||||||
| 60 | 0 | ▶ | if ($cgi->path_info eq '/ping') { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
Branch: 4 branches
| |||||||||||||||||||||||
| 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 | ▶ | unless (defined $name) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | ▶ | unless (defined $name) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | ▶ | unless (Ubic->has_service($name)) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | ▶ | print "HTTP/1.1 500 Internal error\r\n"; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 109 | 0 | print "Content-Type: text/plain; charset=utf-8\r\n\r\n"; | |||||||||||||||||||||
| 110 | 0 | print "Error: $_"; | |||||||||||||||||||||
| 111 | 0 | }; | |||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||
| 113 | |||||||||||||||||||||||
| 114 | 1 | 1; | |||||||||||||||||||||
| 115 | |||||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||||