eb7df68469d671119a6d0a485ecf28b516676bdf
[freeside.git] / httemplate / view / Status.html
1 <& /elements/header.html &>
2 % foreach my $section ( keys %status ) {
3 <FONT CLASS="fsinnerbox-title"><% mt($section) |h %></FONT>
4 <TABLE CLASS="fsinnerbox">
5 %   foreach my $item ( @{ $status{$section} } ) {
6       <TR>
7         <TD ALIGN="right"><% $item->{title} %></TH>
8         <TD><B><% $item->{value} %></B></TD>
9       </TR>
10 %   }
11 </TABLE>
12 <BR><BR>
13 % }
14 <& /elements/footer.html &>
15 <%init>
16
17 my $os;
18 -e '/usr/bin/lsb_release' and run( ['lsb_release', '-d'], '>',\$os );
19 if ( ! $@ && $os =~ /^\s*Description:\s*(.+)$/ ) {
20   $os = $1;
21 } elsif ( my $deb_version = slurp('/etc/debian_version') ) {
22   $os = "Debian $deb_version";
23 }
24
25 ( my $perl_ver = $^V ) =~ s/^v//;
26
27 my $db = driver_name;
28 $db = 'PostgreSQL' if $db =~ /^Pg/;
29 $db = 'MySQL'      if $db =~ /^mysql/;
30
31 my $db_ver = FS::Record->scalar_sql('SELECT VERSION()');
32 if ( $db eq 'PostgreSQL' && $db_ver =~ /^\s*PostgreSQL\s+([\w\.]+)\s+on\s+/ ) {
33   $db_ver = $1;
34 }
35
36 tie my %status, 'Tie::IxHash',
37   'Basics' => [
38     { 'title' => 'Freeside version',
39       'value' => $FS::VERSION,
40     },
41     { 'title' => 'Operating System',
42       'value' => $os,
43     },
44     { 'title' => 'Perl version',
45       'value' => $perl_ver,
46     },
47     { 'title' => 'Database engine',
48       'value' => $db,
49     },
50     { 'title' => 'Database version',
51       'value' => $db_ver,
52     },
53   ],
54   'Required Daemons' => [
55     { 'title' => 'Queue daemon',
56       'value' => _is_running('queued') ? 'Running' : 'Not running',
57     },
58   ],
59   'Optional Daemons' => [
60     { 'title' => 'Self-service server(s)',
61       'value' => '(Not checked)', #XXX multiple pid files, per machine etc
62     },
63     { 'title' => 'Self-service XML-RPC server',
64       'value' => _is_running('selfservice-xmlrpcd') ? 'Running' : 'Not running',
65     },
66     { 'title' => 'Back office XML-RPC server',
67       'value' => _is_running('xmlrpcd') ? 'Running' : 'Not running',
68     },
69     { 'title' => 'RADIUS accounting import daemon',
70       'value' => _is_running('sqlradius-radacctd') ? 'Running' : 'Not running',
71     },
72     { 'title' => 'Prepaid daemon',
73       'value' => _is_running('prepaidd') ? 'Running' : 'Not running',
74     },
75     { 'title' => 'CDR Rewrite daemon',
76       'value' => _is_running('cdrrewrited') ? 'Running' : 'Not running',
77     },
78     { 'title' => 'CDR Prepaid daemon',
79       'value' => _is_running('cdrd') ? 'Running' : 'Not running',
80     },
81     { 'title' => 'CDR Real-time rating daemon',
82       'value' => _is_running('cdrrated') ? 'Running' : 'Not running',
83     },
84     #{ 'title' => 'Network monitoring port combiner', #?
85     #  'value' => _is_running('torrus-srvderive') ? 'Running' : 'Not running',
86     #},
87   ],
88 ;
89
90 if ( $db eq 'PostgreSQL' ) {
91
92   my $enabled =    FS::Record->scalar_sql('SHOW wal_level')    eq 'hot_standby'
93                 && FS::Record->scalar_sql('SHOW archive_mode') eq 'on';
94
95   my $slave = 
96     FS::Record->scalar_sql('SHOW archive_command') =~ / postgres\@([\w\.\-]+): /
97       ? $1 : '';
98
99   $status{'Replication'} = [
100     { 'title' => 'Status', #?
101       'value' => $enabled ? 'Enabled' : 'Disabled',
102     },
103   ];
104
105   if ( $enabled ) {
106     push @{ $status{'Replication'} }, 
107       { 'title' => 'Slave',
108         'value' => $slave || '(Missing, or unparseable archive_command)',
109       },
110     ;
111     if ( $slave ) {
112       #how far behind is it?  will be easier once we're off 9.1
113       # http://www.keithf4.com/monitoring_streaming_slave_lag/
114       # except pg_stat_replication still doesn't fill in the columns we need as
115       # non-Pg user :/
116       push @{ $status{'Replication'} }, 
117         { 'title' => 'Slave',
118           'value' => $slave || '(Missing, or unparseable archive_command)',
119         },
120       ;
121     }
122   }
123
124 } else {
125
126   $status{'Replication'} = [
127     { 'title' => 'Enabled',
128       'value' => "(Not yet checked on $db)",
129     },
130   ];
131
132 }
133
134 if ( _is_running('cdrd') ) {
135
136   my $delay = FS::Record->scalar_sql('
137     SELECT AVG(end_date-insert_date)
138       FROM queue_stat
139       GROUP BY statnum
140       ORDER BY statnum DESC
141       LIMIT 100
142   ');
143   if ( $delay ) {
144     my $h = int($delay/3600);
145     my $m = int( ($delay%3600) / 60 );
146     my $s = $delay%60;
147
148     $delay = ( $h     ? $h. 'h' : '' ).
149              ( $h||$m ? $m. 'm' : '').
150                         $s. 's';
151
152   }
153
154   my $pr_delay = FS::Record->scalar_sql('
155     SELECT AVG(end_date-start_date)
156       FROM queue_stat
157       GROUP BY statnum
158       ORDER BY statnum DESC
159       LIMIT 100
160   ');
161   if ( $pr_delay ) {
162     my $h = int($delay/3600);
163     my $m = int( ($delay%3600) / 60 );
164     my $s = $delay%60;
165
166     $pr_delay = ( $h     ? $h. 'h' : '' ).
167                 ( $h||$m ? $m. 'm' : '').
168                            $s. 's';
169
170   }
171
172   my $dayago = time2str('%Y-%m-%d %X', time - 86400);
173   my $cdrs = FS::Record->scalar_sql(qq{
174     SELECT COUNT(*) FROM cdr
175       WHERE ( freesidestatus IS NULL OR freesidestatus = '' )
176         AND calldate > '$dayago'
177   });
178
179   $status{'CDR Processing'} = [
180     { 'title' => 'Current processing delay',
181       'value' => $delay,
182     },
183     { 'title' => 'Average billing time',
184       'value' => $pr_delay,
185     },
186     { 'title' => 'Unprocessed CDRs (last 24 hours)',
187       'value' => $cdrs,
188     },
189   ];
190
191 }
192
193 sub _is_running {
194   my $thing = shift;
195
196   my $pid_path = '/var/run'; #XXX hardcoded path
197
198   my $pidfile =
199     -e "$pid_path/freeside/$thing.pid" ? "$pid_path/freeside/$thing.pid" :
200     -e "$pid_path/freeside-$thing.pid" ? "$pid_path/freeside-$thing.pid" :
201    return 0;
202
203   -e $pidfile and kill 0, slurp($pidfile)
204 }
205
206 </%init>