2012-02-08 23:14:28 +01:00
< ? php
class ModelHealthHealth extends Model {
2020-03-14 13:19:47 +01:00
public $data = [];
2020-03-14 12:38:27 +01:00
public function collect_data () {
ini_set ( " default_socket_timeout " , 5 );
list ( $this -> data [ 'uptime' ], $this -> data [ 'cpuload' ]) = $this -> uptime ();
exec ( PILER_BINARY . " -v " , $a );
$this -> data [ 'piler_version' ] = $a [ 0 ];
$x = exec ( CPU_USAGE_COMMAND );
$this -> data [ 'cpuinfo' ] = 100 - ( int ) $x ;
list ( $this -> data [ 'totalmem' ], $this -> data [ 'meminfo' ], $this -> data [ 'totalswap' ], $this -> data [ 'swapinfo' ]) = $this -> meminfo ();
$this -> data [ 'shortdiskinfo' ] = $this -> diskinfo ();
2020-03-14 13:19:47 +01:00
list ( $archivesizeraw , $archivestoredsizeraw , $this -> data [ 'counters' ]) = $this -> model_stat_counter -> get_counters ();
2020-03-14 12:38:27 +01:00
$this -> data [ 'archive_size' ] = nice_size ( $archivesizeraw , ' ' );
$this -> data [ 'archive_stored_size' ] = nice_size ( $archivestoredsizeraw , ' ' );
$this -> data [ 'prefix' ] = '' ;
if ( isset ( $this -> data [ 'counters' ][ MEMCACHED_PREFIX . 'rcvd' ])) { $this -> data [ 'prefix' ] = MEMCACHED_PREFIX ; }
$this -> data [ 'sysinfo' ] = $this -> model_health_health -> sysinfo ();
$this -> data [ 'options' ] = $this -> model_health_health -> get_options ();
$this -> data [ 'indexer_stat' ] = $this -> indexer_stat ();
$this -> data [ 'purge_stat' ] = $this -> purge_stat ();
2023-01-29 14:06:15 +01:00
$this -> data [ 'sphinx_current_main_size' ] = $this -> get_index_size ( SPHINX_CURRENT_MAIN_INDEX_SIZE );
$this -> data [ 'sphinx_total_size' ] = $this -> get_index_size ( SPHINX_TOTAL_INDEX_SIZE );
2020-03-14 12:38:27 +01:00
$this -> get_average_count_values ();
$this -> get_average_size_values ( $archivesizeraw );
if ( ENABLE_SAAS == 1 ) {
$this -> data [ 'num_of_online_users' ] = $this -> model_saas_customer -> count_online ();
}
$this -> data [ 'health' ] = [];
2020-03-14 13:19:47 +01:00
$lang = Registry :: get ( 'language' );
2020-03-14 12:38:27 +01:00
foreach ( Registry :: get ( 'health_smtp_servers' ) as $smtp ) {
if ( $smtp [ 0 ]) {
$this -> data [ 'health' ][] = $this -> checksmtp ( $smtp , $lang -> data [ 'text_error' ]);
}
}
}
public function get_average_count_values () {
$this -> data [ 'processed_emails' ] = $this -> count_processed_emails ();
// average messages per day, computed over the past week
2020-03-14 13:19:47 +01:00
$this -> data [ 'averagemessagesweekraw' ] = ( $this -> data [ 'processed_emails' ][ 'last_7_days_count' ]) / 7 ;
2020-03-14 12:38:27 +01:00
// average messages per day, computed over the past month
2020-03-14 13:19:47 +01:00
$this -> data [ 'averagemessagesmonthraw' ] = ( $this -> data [ 'processed_emails' ][ 'last_30_days_count' ]) / 30 ;
2020-03-14 12:38:27 +01:00
//average messages per day, computed over the time period since the first email was archived
$total_number_days = round ( ( time () - $this -> get_first_email_arrival_ts ()) / 86400 );
if ( $total_number_days == 0 ) {
$total_number_days = 1 ;
}
2020-03-14 13:19:47 +01:00
$this -> data [ 'averagemessagestotalraw' ] = $this -> data [ 'counters' ][ 'rcvd' ] / $total_number_days ;
2020-03-14 12:38:27 +01:00
}
public function get_average_size_values ( $archivesizeraw = 0 ) {
$averagemessagesizeraw = $averagesqlsizeraw = $averagesphinxsizeraw = 0 ;
if ( $this -> data [ 'counters' ][ 'rcvd' ] > 0 ) {
// average message size, computed for total messages in database
$averagemessagesizeraw = $archivesizeraw / $this -> data [ 'counters' ][ 'rcvd' ];
//average message metadata size, computed for total messages in database
$averagesqlsizeraw = $this -> get_database_size () / $this -> data [ 'counters' ][ 'rcvd' ];
//average message sphinx index size, computed for total messages in database
2023-01-29 14:06:15 +01:00
$averagesphinxsizeraw = $this -> data [ 'sphinx_total_size' ] / $this -> data [ 'counters' ][ 'rcvd' ];
2020-03-14 12:38:27 +01:00
}
// average total message size per day, computed over the time period since the first email was archived
2020-07-05 14:12:32 +02:00
$averagesizedayraw = ( $averagemessagesizeraw + $averagesqlsizeraw + $averagesphinxsizeraw ) * $this -> data [ 'averagemessagesmonthraw' ];
2020-03-14 12:38:27 +01:00
$datapart = 0 ;
foreach ( $this -> data [ 'shortdiskinfo' ] as $part ) {
if ( $part [ 'partition' ] == DATA_PARTITION ) {
$datapart = $part [ 'freespace' ] * 1024 ;
}
}
$this -> data [ 'oldestmessagets' ] = $this -> get_oldest_record_ts (); // date of the oldest record in the db
2020-03-14 13:19:47 +01:00
$this -> data [ 'averagemessages' ] = round ( $this -> data [ 'averagemessagesweekraw' ]); // rounded average of messages over the past week
2020-03-14 12:38:27 +01:00
$this -> data [ 'averagemessagesize' ] = nice_size ( $averagemessagesizeraw , ' ' ); // formatted average message size on disk
$this -> data [ 'averagesqlsize' ] = nice_size ( $averagesqlsizeraw , ' ' ); // formatted average metadata size in sql
$this -> data [ 'averagesphinxsize' ] = nice_size ( $averagesphinxsizeraw , ' ' ); // formatted average sphinx index
$this -> data [ 'averagesizeday' ] = nice_size ( $averagesizedayraw , ' ' ); // formatted average size per day
// estimated number of days of free space left
$averagesizedayraw > 0 ? $this -> data [ 'daysleftatcurrentrate' ] = convert_days_ymd ( $datapart / $averagesizedayraw ) : $this -> data [ 'daysleftatcurrentrate' ] = 0 ;
/*
* determine if the trend of the last week compared to the last month is
* increasing , decreasing , or neutral ( only applies to message count , not size )
*/
2020-03-14 13:19:47 +01:00
if ( $this -> data [ 'averagemessagesweekraw' ] > $this -> data [ 'averagemessagesmonthraw' ]) {
2020-03-14 12:38:27 +01:00
$this -> data [ 'usagetrend' ] = 1 ;
2020-03-14 13:19:47 +01:00
} elseif ( $this -> data [ 'averagemessagesweekraw' ] < $this -> data [ 'averagemessagesmonthraw' ]) {
2020-03-14 12:38:27 +01:00
$this -> data [ 'usagetrend' ] = - 1 ;
} else {
$this -> data [ 'usagetrend' ] = 0 ;
}
}
2012-02-08 23:14:28 +01:00
public function format_time ( $time = 0 ) {
if ( $time >= 1 ) {
return sprintf ( " %.2f " , $time ) . " sec " ;
}
else {
return sprintf ( " %.2f " , 1000 * $time ) . " ms " ;
}
}
public function checksmtp ( $smtp = array (), $error = '' ) {
$ret = $error ;
$time = 0 ;
2015-05-12 14:34:38 +02:00
$time_start = microtime ( true );
2012-02-08 23:14:28 +01:00
if ( $smtp [ 0 ] && $smtp [ 1 ] && is_numeric ( $smtp [ 1 ]) && $smtp [ 1 ] > 0 && $smtp [ 1 ] < 65536 ) {
$s = @ fsockopen ( $smtp [ 0 ], $smtp [ 1 ]);
if ( $s ) {
$ret = trim ( fgets ( $s , 4096 ));
fputs ( $s , " QUIT \r \n " );
fclose ( $s );
}
}
$time = microtime ( true ) - $time_start ;
return array ( $smtp [ 0 ] . " : " . $smtp [ 1 ], $ret , $this -> format_time ( $time ), $smtp [ 2 ]);
}
public function count_processed_emails () {
$today = $last_7_days = $last_30_days = 0 ;
2014-10-23 09:31:40 +02:00
$a = array ();
2012-02-08 23:14:28 +01:00
$now = time ();
2013-08-22 11:24:54 +02:00
$ts = $now - 3600 ;
2015-05-06 12:21:41 +02:00
$query = $this -> db -> query ( " select count(*) as count from " . TABLE_META . " where arrived > $ts " );
2014-10-23 09:31:40 +02:00
if ( isset ( $query -> row [ 'count' ])) {
$a [ 'last_60_mins_count' ] = $query -> row [ 'count' ];
}
2013-08-22 11:24:54 +02:00
2012-02-08 23:14:28 +01:00
$ts = $now - 86400 ;
2015-05-06 12:21:41 +02:00
$query = $this -> db -> query ( " select count(*) as count from " . TABLE_META . " where arrived > $ts " );
2014-10-23 09:31:40 +02:00
if ( isset ( $query -> row [ 'count' ])) {
$a [ 'today_count' ] = $query -> row [ 'count' ];
}
2012-02-08 23:14:28 +01:00
$ts = $now - 604800 ;
2015-05-06 12:21:41 +02:00
$query = $this -> db -> query ( " select count(*) as count from " . TABLE_META . " where arrived > $ts " );
2014-10-23 09:31:40 +02:00
if ( isset ( $query -> row [ 'count' ])) {
$a [ 'last_7_days_count' ] = $query -> row [ 'count' ];
}
2012-02-08 23:14:28 +01:00
$ts = $now - 2592000 ;
2015-05-06 12:21:41 +02:00
$query = $this -> db -> query ( " select count(*) as count from " . TABLE_META . " where arrived > $ts " );
2014-10-23 09:31:40 +02:00
if ( isset ( $query -> row [ 'count' ])) {
$a [ 'last_30_days_count' ] = $query -> row [ 'count' ];
}
2012-02-08 23:14:28 +01:00
2014-10-23 09:31:40 +02:00
return $a ;
2012-02-08 23:14:28 +01:00
}
public function uptime () {
$s = exec ( " uptime " );
list ( $uptime , $loadavg ) = preg_split ( " / load average \ : / " , $s );
return array ( preg_replace ( " / \ , \ { 0,} $ / " , " " , $uptime ), $loadavg );
}
public function meminfo () {
$m = explode ( " \n " , file_get_contents ( " /proc/meminfo " ));
2022-01-19 15:40:42 +01:00
foreach ( $m as $k => $v ) {
2012-02-08 23:14:28 +01:00
$a = preg_split ( " / \ { 1,}/ " , $v );
if ( isset ( $a [ 0 ]) && $a [ 0 ]) { $_m [ $a [ 0 ]] = $a [ 1 ]; }
}
2012-09-14 11:04:43 +02:00
$mem_percentage = isset ( $_m [ 'MemTotal:' ]) && $_m [ 'MemTotal:' ] > 0 ? sprintf ( " %.2f " , 100 * ( $_m [ 'MemTotal:' ] - $_m [ 'MemFree:' ] - $_m [ 'Cached:' ]) / $_m [ 'MemTotal:' ]) : " 0 " ;
$swap_percentage = isset ( $_m [ 'SwapTotal:' ]) && $_m [ 'SwapTotal:' ] > 0 ? sprintf ( " %.2f " , 100 * ( $_m [ 'SwapTotal:' ] - $_m [ 'SwapFree:' ]) / $_m [ 'SwapTotal:' ]) : " 0 " ;
2012-02-08 23:14:28 +01:00
2012-09-11 15:12:58 +02:00
return array ( sprintf ( " %.0f " , @ $_m [ 'MemTotal:' ] / 1000 ), $mem_percentage , sprintf ( " %.0f " , @ $_m [ 'SwapTotal:' ] / 1000 ), $swap_percentage );
2012-02-08 23:14:28 +01:00
}
public function diskinfo () {
$shortinfo = array ();
2013-08-20 12:05:38 +02:00
$a = array ();
2012-02-08 23:14:28 +01:00
2013-02-16 12:33:25 +01:00
$s = exec ( " df " , $output );
2012-02-08 23:14:28 +01:00
$partitions = Registry :: get ( 'partitions_to_monitor' );
2022-01-19 15:40:42 +01:00
foreach ( $output as $k => $v ) {
2012-02-08 23:14:28 +01:00
if ( $k > 0 ) {
$p = preg_split ( " / \ { 1,}/ " , $v );
2013-08-20 12:05:38 +02:00
if ( isset ( $p [ 5 ]) && in_array ( $p [ 5 ], $partitions ) && ! isset ( $a [ $p [ 5 ]])) {
$a [ $p [ 5 ]] = 1 ;
2012-02-08 23:14:28 +01:00
$shortinfo [] = array (
'partition' => $p [ 5 ],
2013-08-01 22:28:42 +02:00
'freespace' => $p [ 3 ],
'total' => $p [ 1 ],
'used' => $p [ 2 ],
2012-02-08 23:14:28 +01:00
'utilization' => preg_replace ( " / \ %/ " , " " , $p [ 4 ])
);
}
}
}
return $shortinfo ;
}
public function sysinfo () {
$hostname = exec ( " hostname -f " );
$s = exec ( " uname -a " );
return array ( $hostname , $s );
}
2012-04-27 14:39:10 +02:00
public function get_options () {
$data = array ();
$query = $this -> db -> query ( " SELECT * FROM ` " . TABLE_OPTION . " ` " );
if ( isset ( $query -> rows )) {
foreach ( $query -> rows as $q ) {
$data [ $q [ 'key' ]] = $q [ 'value' ];
}
}
return $data ;
}
public function toggle_option ( $option = '' ) {
$value = 0 ;
$query = $this -> db -> query ( " SELECT `value` FROM ` " . TABLE_OPTION . " ` WHERE `key`=? " , array ( $option ));
if ( isset ( $query -> row [ 'value' ])) {
if ( $query -> row [ 'value' ] == 0 ) { $value = 1 ; }
else { $value = 0 ; }
$query = $this -> db -> query ( " UPDATE ` " . TABLE_OPTION . " ` SET `value`=? WHERE `key`=? " , array ( $value , $option ));
}
}
2013-04-22 23:03:40 +02:00
2013-02-16 12:33:25 +01:00
public function get_database_size () {
$data = array ();
$query = $this -> db -> query ( " SELECT table_schema AS `name`,
SUM ( data_length + index_length ) AS `size`
FROM information_schema . TABLES
WHERE table_schema = '".DB_DATABASE."'
GROUP BY table_schema ; " );
if ( isset ( $query -> rows )) {
2013-04-22 23:03:40 +02:00
$data = array_pop ( $query -> rows );
} else {
$data [ 'size' ] = 0 ;
2013-02-16 12:33:25 +01:00
}
return $data [ 'size' ];
}
2013-04-22 23:03:40 +02:00
public function get_oldest_record_ts () {
$data = array ();
2013-10-19 10:39:10 +02:00
$query = $this -> db -> query ( " SELECT `sent` AS `oldest_record_ts` FROM " . TABLE_META . " WHERE `deleted`=0 and `sent` > 837381600 ORDER BY `sent` ASC LIMIT 1 " );
2013-04-22 23:03:40 +02:00
if ( isset ( $query -> rows )) {
$data = array_pop ( $query -> rows );
} else {
$data [ 'oldest_record_ts' ] = 0 ;
}
return $data [ 'oldest_record_ts' ];
}
2014-04-25 13:42:08 +02:00
public function get_first_email_arrival_ts () {
$query = $this -> db -> query ( " SELECT `arrived` FROM " . TABLE_META . " ORDER BY id ASC LIMIT 1 " );
2015-03-23 15:48:06 +01:00
if ( isset ( $query -> row [ 'arrived' ])) { return $query -> row [ 'arrived' ]; }
2014-04-25 13:42:08 +02:00
return time ();
}
2013-08-09 10:13:54 +02:00
public function indexer_stat () {
$data = array ( '' , '' );
if ( file_exists ( INDEXER_BEACON )) {
$st = stat ( INDEXER_BEACON );
$t1 = date ( DATE_TEMPLATE . " H:i " , $st [ 'mtime' ]);
2019-08-25 12:33:53 +02:00
$t2 = date ( DATE_TEMPLATE . " H:i " , $st [ 'mtime' ] + DELTA_INDEXER_PERIOD );
2013-08-09 10:13:54 +02:00
$data = array ( $t1 , $t2 );
}
return $data ;
}
2013-08-11 09:22:03 +02:00
public function purge_stat () {
$data = array ( '' , '' );
if ( file_exists ( PURGE_BEACON )) {
$st = stat ( PURGE_BEACON );
$t1 = date ( DATE_TEMPLATE . " H:i " , $st [ 'mtime' ]);
$t2 = date ( DATE_TEMPLATE . " H:i " , $st [ 'mtime' ] + 86400 );
$data = array ( $t1 , $t2 );
}
return $data ;
}
2012-02-08 23:14:28 +01:00
2023-01-29 14:06:15 +01:00
public function get_index_size ( $statfile = '' ) {
2018-03-18 13:54:13 +01:00
$size = 0 ;
2023-01-29 14:06:15 +01:00
if ( file_exists ( $statfile )) {
$size = ( int ) file_get_contents ( $statfile );
2018-03-18 13:54:13 +01:00
}
return $size ;
}
2012-02-08 23:14:28 +01:00
2018-03-18 13:54:13 +01:00
}