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