File Coverage

blib/lib/Ubic/Watchdog/Notice.pm
Criterion Covered Total %
statement 24 62 38.7
branch 0 18 0.0
condition 0 5 0.0
subroutine 8 10 80.0
pod 0 2 0.0
total 32 97 32.9


line stmt bran cond sub pod time code
1             package Ubic::Watchdog::Notice;
2            
3 1     1   24698 use strict;
  1         3  
  1         42  
4 1     1   7 use warnings;
  1         2  
  1         32  
5              
6 1     1   1109 use Sys::Hostname;
  1         1300  
  1         52  
7 1     1   1226 use Getopt::Long;
  1         16094  
  1         8  
8 1     1   1154 use File::Tail;
  1         42675  
  1         67  
9 1     1   1454 use MIME::Lite;
  1         26502  
  1         40  
10 1     1   892 use URI;
  1         6087  
  1         33  
11 1     1   1082 use LWP::UserAgent;
  1         41510  
  1         766  
12              
13             our $VERSION = 0.31;
14              
15             my $host = hostname;
16              
17             my $ua = LWP::UserAgent->new;
18             $ua->timeout(10);
19              
20             $SIG{TERM} = sub { exit 0 };
21              
22             my $conf;
23             my $conf_file = '/etc/ubic/notice.cfg';
24              
25             my $default = {
26             log => '/var/log/ubic/watchdog.log',
27             hipchat => {
28             host => 'https://api.hipchat.com',
29             },
30             slack => {
31             host => 'https://slack.com',
32             },
33             };
34              
35             sub run {
36 0     0 0   GetOptions(
37             'config=s' => \$conf_file,
38             );
39              
40 0 0         die "Configuration file <$conf_file> not exists" unless -e $conf_file;
41              
42 0           $conf = do $conf_file;
43              
44 0           for (qw/From To/) {
45 0 0         die "Configuration value <$_> is required" unless $conf->{$_};
46             }
47              
48 0   0       $conf->{log} ||= $default->{log};
49              
50 0           while (1) {
51             # Maybe file not exists
52 0           eval {
53 0           my $F = File::Tail->new(name => $conf->{log}, maxinterval => 1);
54 0           my $line;
55 0           while (defined( $line = $F->read )) {
56 0 0         if (my ($service) = $line =~ /\]\s*(\S+)\s+status.*restarting/) {
57 0           notice($service, $line);
58             }
59             }
60             };
61              
62 0           sleep 1;
63             }
64             }
65              
66             sub notice {
67 0     0 0   my ($service, $txt) = @_;
68              
69 0 0         return unless $service;
70              
71             my $msg = MIME::Lite->new(
72             From => $conf->{From},
73             To => $conf->{To },
74 0           Subject => "[UBIC] $service down on $host",
75             Data => $txt,
76             );
77 0           $msg->attr("content-type.charset" => "utf-8");
78 0           $msg->send("sendmail", "/usr/sbin/sendmail -t -oi -oem");
79              
80 0 0         if ($conf->{hipchat}) {
81 0           my $h = $host;
82 0 0         $h = substr $h, 0, 15 if length($h) > 15;
83              
84             my $response = $ua->post("$default->{hipchat}->{host}/v1/rooms/message", {
85             auth_token => $conf->{hipchat}->{token},
86             room_id => $conf->{hipchat}->{room },
87 0           from => $h,
88             message => $txt,
89             message_format => 'text',
90             notify => 1,
91             color => 'yellow',
92             format => 'json',
93             });
94              
95 0 0         unless ($response->is_success) {
96 0           warn "Hipchat notification failed!";
97 0           warn $response->status_line;
98 0           warn $response->content;
99             }
100             }
101              
102 0 0         if($conf->{slack}) {
103 0           my $t = $conf->{slack};
104 0           $t->{text } = "[$service] down on $host";
105 0   0       $t->{username} ||= 'Ubic Server Bot';
106              
107 0           my $url = URI->new("$default->{slack}->{host}/api/chat.postMessage");
108 0           $url->query_form(%$t);
109 0           my $response = $ua->get($url);
110              
111 0 0         unless ($response->is_success) {
112 0           warn "Slack notification failed!";
113 0           warn $response->status_line;
114 0           warn $response->content;
115             }
116             }
117             }
118              
119             1;
120              
121             =pod
122            
123             =head1 NAME
124              
125             Ubic::Watchdog::Notice - Notice service for ubic.
126              
127             =head1 VERSION
128              
129             version 0.31
130              
131             =head1 SYNOPSIS
132              
133             Start notice service:
134             $ ubic start ubic.notice
135              
136             =head1 DESCRIPTION
137              
138             Currently module can notice by email and to L or L service.
139              
140             =head1 INSTALLATION
141              
142             Put this code in file `/etc/ubic/service/ubic/notice`:
143              
144             use Ubic::Service::SimpleDaemon;
145            
146             Ubic::Service::SimpleDaemon->new(
147             bin => ['ubic-notice'],
148             );
149              
150             Put this configuration in file `/etc/ubic/notice.cfg`:
151              
152              
153             {
154             From => 'likhatskiy@gmail.com',
155             To => 'name@mail.com',
156             };
157              
158             Start it:
159              
160             $ ubic start ubic.notice
161              
162             =head1 OPTIONS
163              
164             =over
165              
166             =item B< From >
167            
168             Sets the email address to send from.
169              
170             =item B< To >
171            
172             Sets the addresses in `MIME::Lite` style to send to.
173              
174             =item B< log >
175            
176             Path to `ubic-watchdog` file for scan. Default is `/var/log/ubic/watchdog.log`.
177              
178             =item B< hipchat >
179            
180             Notice to L service.
181              
182             hipchat => {
183             token => 'YOUR_TOKEN',
184             room => 'ROOM_NAME'
185             },
186              
187             =item B< slack >
188            
189             Notice to L service.
190              
191             slack => {
192             token => 'YOUR_TOKEN',
193             channel => '#CHANNEL_NAME'
194             username => 'Ubic Server Bot'
195             },
196              
197             =back
198              
199             =head1 SOURCE REPOSITORY
200              
201             L
202              
203             =head1 AUTHOR
204              
205             Alexey Likhatskiy,
206              
207             =head1 LICENSE AND COPYRIGHT
208              
209             Copyright (C) 2014 "Alexey Likhatskiy"
210              
211             This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.