2016-09-14 10:53:04 -05:00
#!/usr/bin/env php
2013-11-03 22:16:01 -08:00
< ? php
/*
* Daily Task Checks
* ( c ) 2013 LibreNMS Contributors
*/
2017-10-22 13:30:31 -05:00
use LibreNMS\Config ;
2017-11-24 03:37:52 -06:00
use LibreNMS\Exceptions\LockException ;
use LibreNMS\Util\MemcacheLock ;
2017-10-22 13:30:31 -05:00
2016-11-21 14:12:59 -06:00
$init_modules = array ( 'alerts' );
require __DIR__ . '/includes/init.php' ;
2017-08-26 15:35:39 -05:00
include_once __DIR__ . '/includes/notifications.php' ;
2013-11-03 22:16:01 -08:00
2017-11-24 03:37:52 -06:00
$options = getopt ( 'df:o:t:r:' );
2016-01-11 14:58:40 -07:00
if ( isset ( $options [ 'd' ])) {
echo " DEBUG \n " ;
$debug = true ;
}
2013-11-03 22:16:01 -08:00
2014-10-25 08:40:42 +10:00
if ( $options [ 'f' ] === 'update' ) {
2016-02-23 09:10:56 +11:00
if ( ! $config [ 'update' ]) {
exit ( 0 );
}
if ( $config [ 'update_channel' ] == 'master' ) {
exit ( 1 );
2016-08-28 17:32:55 -05:00
} elseif ( $config [ 'update_channel' ] == 'release' ) {
2016-02-23 09:10:56 +11:00
exit ( 3 );
2015-07-29 22:03:25 +00:00
}
2016-02-23 09:10:56 +11:00
exit ( 0 );
2014-10-25 08:40:42 +10:00
}
2013-11-03 22:16:01 -08:00
2017-10-22 13:30:31 -05:00
if ( $options [ 'f' ] === 'check_php_ver' ) {
$min_version = '5.6.4' ;
$warn_title = 'Warning: PHP version too low' ;
// if update is not set to false and version is min or newer
if ( Config :: get ( 'update' ) && version_compare ( PHP_VERSION , $min_version , '<' )) {
new_notification (
$warn_title ,
'PHP version 5.6.4 will be the minimum supported version on January 10, 2018. We recommend you update to PHP a supported version of PHP (7.1 suggested) to continue to receive updates. If you do not update PHP, LibreNMS will continue to function but stop receiving bug fixes and updates.' ,
1 ,
'daily.sh'
);
exit ( 1 );
}
remove_notification ( $warn_title );
exit ( 0 );
}
2016-07-30 16:57:09 +01:00
if ( $options [ 'f' ] === 'rrd_purge' ) {
2017-11-24 03:37:52 -06:00
try {
if ( Config :: get ( 'distributed_poller' )) {
MemcacheLock :: lock ( 'rrd_purge' , 0 , 86000 );
2016-08-01 15:08:57 +01:00
}
2017-11-24 03:37:52 -06:00
$rrd_purge = Config :: get ( 'rrd_purge' );
$rrd_dir = Config :: get ( 'rrd_dir' );
if ( is_numeric ( $rrd_purge ) && $rrd_purge > 0 ) {
$cmd = " find $rrd_dir -type f -mtime + $rrd_purge -print -exec rm -f { } + " ;
$purge = `$cmd` ;
if ( ! empty ( $purge )) {
echo " Purged the following RRD files due to old age (over $rrd_purge days old): \n " ;
echo $purge ;
}
}
} catch ( LockException $e ) {
echo $e -> getMessage () . PHP_EOL ;
exit ( - 1 );
2016-07-30 16:57:09 +01:00
}
}
2014-10-25 08:40:42 +10:00
if ( $options [ 'f' ] === 'syslog' ) {
2017-11-24 03:37:52 -06:00
try {
if ( Config :: get ( 'distributed_poller' )) {
MemcacheLock :: lock ( 'syslog_purge' , 0 , 86000 );
}
$syslog_purge = Config :: get ( 'syslog_purge' );
if ( is_numeric ( $syslog_purge )) {
$rows = ( int ) dbFetchCell ( 'SELECT MIN(seq) FROM syslog' );
while ( true ) {
$limit = dbFetchRow ( 'SELECT seq FROM syslog WHERE seq >= ? ORDER BY seq LIMIT 1000,1' , array ( $rows ));
if ( empty ( $limit )) {
break ;
}
2015-07-13 20:10:26 +02:00
2017-11-24 03:37:52 -06:00
if ( dbDelete ( 'syslog' , 'seq >= ? AND seq < ? AND timestamp < DATE_SUB(NOW(), INTERVAL ? DAY)' , array ( $rows , $limit , $syslog_purge )) > 0 ) {
$rows = $limit ;
echo " Syslog cleared for entries over $syslog_purge days 1000 limit \n " ;
} else {
break ;
}
2015-07-13 20:10:26 +02:00
}
2017-11-24 03:37:52 -06:00
dbDelete ( 'syslog' , 'seq >= ? AND timestamp < DATE_SUB(NOW(), INTERVAL ? DAY)' , array ( $rows , $syslog_purge ));
}
} catch ( LockException $e ) {
echo $e -> getMessage () . PHP_EOL ;
exit ( - 1 );
2014-03-18 14:13:27 +00:00
}
}
2015-07-13 20:10:26 +02:00
2014-10-25 08:40:42 +10:00
if ( $options [ 'f' ] === 'eventlog' ) {
2017-11-24 03:37:52 -06:00
$ret = lock_and_purge ( 'eventlog' , 'datetime < DATE_SUB(NOW(), INTERVAL ? DAY)' );
exit ( $ret );
2014-03-18 14:13:27 +00:00
}
2015-07-13 20:10:26 +02:00
2014-10-25 08:40:42 +10:00
if ( $options [ 'f' ] === 'authlog' ) {
2017-11-24 03:37:52 -06:00
$ret = lock_and_purge ( 'authlog' , 'datetime < DATE_SUB(NOW(), INTERVAL ? DAY)' );
exit ( $ret );
2014-10-02 01:37:43 +01:00
}
2015-07-13 20:10:26 +02:00
2014-11-03 18:25:45 +01:00
if ( $options [ 'f' ] === 'perf_times' ) {
2017-11-24 03:37:52 -06:00
$ret = lock_and_purge ( 'perf_times' , 'start < UNIX_TIMESTAMP(DATE_SUB(NOW(),INTERVAL ? DAY))' );
exit ( $ret );
2014-11-03 18:25:45 +01:00
}
2015-07-13 20:10:26 +02:00
2015-04-06 20:47:28 +01:00
if ( $options [ 'f' ] === 'callback' ) {
2016-01-19 21:50:00 +00:00
include_once 'includes/callback.php' ;
2015-04-06 20:47:28 +01:00
}
2015-07-13 20:10:26 +02:00
2015-06-22 21:55:31 +01:00
if ( $options [ 'f' ] === 'device_perf' ) {
2017-11-24 03:37:52 -06:00
$ret = lock_and_purge ( 'device_perf' , 'timestamp < DATE_SUB(NOW(),INTERVAL ? DAY)' );
exit ( $ret );
2015-06-22 21:55:31 +01:00
}
2015-09-27 15:35:33 +00:00
2017-10-22 13:30:31 -05:00
if ( $options [ 'f' ] === 'handle_notifiable' ) {
2017-08-26 15:35:39 -05:00
if ( $options [ 't' ] === 'update' ) {
$title = 'Error: Daily update failed' ;
2017-11-24 03:37:52 -06:00
$poller_name = Config :: get ( 'distributed_poller_name' );
2017-08-26 15:35:39 -05:00
if ( $options [ 'r' ]) {
2017-10-22 13:30:31 -05:00
// result was a success (1), remove the notification
2017-08-26 15:35:39 -05:00
remove_notification ( $title );
} else {
2017-10-22 13:30:31 -05:00
// result was a failure (0), create the notification
2017-08-26 15:35:39 -05:00
new_notification (
$title ,
2017-11-24 03:37:52 -06:00
" The daily update script (daily.sh) has failed on $poller_name . "
. 'Please check output by hand. If you need assistance, '
2017-08-26 15:35:39 -05:00
. 'visit the <a href="https://www.librenms.org/#support">LibreNMS Website</a> to find out how.' ,
2 ,
'daily.sh'
);
}
}
}
2015-09-27 15:35:33 +00:00
if ( $options [ 'f' ] === 'notifications' ) {
2017-11-24 03:37:52 -06:00
try {
if ( Config :: get ( 'distributed_poller' )) {
MemcacheLock :: lock ( 'notifications' , 0 , 86000 );
}
post_notifications ();
} catch ( LockException $e ) {
echo $e -> getMessage () . PHP_EOL ;
exit ( - 1 );
}
2015-09-27 15:35:33 +00:00
}
2015-12-13 16:16:39 +00:00
2016-03-02 18:39:16 +00:00
if ( $options [ 'f' ] === 'bill_data' ) {
2017-11-24 03:37:52 -06:00
try {
if ( Config :: get ( 'distributed_poller' )) {
MemcacheLock :: lock ( 'syslog_purge' , 0 , 86000 );
}
$billing_data_purge = Config :: get ( 'billing_data_purge' );
if ( is_numeric ( $billing_data_purge ) && $billing_data_purge > 0 ) {
# Deletes data older than XX months before the start of the last complete billing period
echo " Deleting billing data more than $billing_data_purge month before the last completed billing cycle \n " ;
$sql = " DELETE bill_data
FROM bill_data
INNER JOIN ( SELECT bill_id ,
2016-03-02 18:39:16 +00:00
SUBDATE (
2017-11-24 03:37:52 -06:00
SUBDATE (
ADDDATE (
subdate ( curdate (), ( day ( curdate ()) - 1 )), # Start of this month
bill_day - 1 ), # Billing anniversary
INTERVAL IF ( bill_day > DAY ( curdate ()), 1 , 0 ) MONTH ), # Deal with anniversary not yet happened this month
INTERVAL ? MONTH ) AS threshold # Adjust based on config threshold
FROM bills ) q
ON bill_data . bill_id = q . bill_id AND bill_data . timestamp < q . threshold ; " ;
dbQuery ( $sql , array ( $billing_data_purge ));
}
} catch ( LockException $e ) {
echo $e -> getMessage () . PHP_EOL ;
exit ( - 1 );
2016-03-02 18:39:16 +00:00
}
}
2016-05-02 19:06:51 +00:00
if ( $options [ 'f' ] === 'alert_log' ) {
2017-11-24 03:37:52 -06:00
$ret = lock_and_purge ( 'alert_log' , 'time_logged < DATE_SUB(NOW(),INTERVAL ? DAY)' );
exit ( $ret );
2016-05-02 19:06:51 +00:00
}
2015-12-13 16:16:39 +00:00
if ( $options [ 'f' ] === 'purgeusers' ) {
2017-11-24 03:37:52 -06:00
try {
if ( Config :: get ( 'distributed_poller' )) {
MemcacheLock :: lock ( 'purgeusers' , 0 , 86000 );
}
$purge = 0 ;
if ( is_numeric ( $config [ 'radius' ][ 'users_purge' ]) && $config [ 'auth_mechanism' ] === 'radius' ) {
$purge = $config [ 'radius' ][ 'users_purge' ];
2015-12-13 16:16:39 +00:00
}
2017-11-24 03:37:52 -06:00
if ( is_numeric ( $config [ 'active_directory' ][ 'users_purge' ]) && $config [ 'auth_mechanism' ] === 'active_directory' ) {
$purge = $config [ 'active_directory' ][ 'users_purge' ];
2015-12-13 16:16:39 +00:00
}
2017-11-24 03:37:52 -06:00
if ( $purge > 0 ) {
foreach ( dbFetchRows ( " SELECT DISTINCT(`user`) FROM `authlog` WHERE `datetime` >= DATE_SUB(NOW(), INTERVAL ? DAY) " , array ( $purge )) as $user ) {
$users [] = $user [ 'user' ];
}
$del_users = '"' . implode ( '","' , $users ) . '"' ;
if ( dbDelete ( 'users' , " username NOT IN ( $del_users ) " , array ( $del_users ))) {
echo " Removed users that haven't logged in for $purge days " ;
}
}
} catch ( LockException $e ) {
echo $e -> getMessage () . PHP_EOL ;
exit ( - 1 );
2015-12-13 16:16:39 +00:00
}
}
2016-10-15 00:29:55 +01:00
if ( $options [ 'f' ] === 'refresh_alert_rules' ) {
2017-11-24 03:37:52 -06:00
try {
if ( Config :: get ( 'distributed_poller' )) {
MemcacheLock :: lock ( 'refresh_alert_rules' , 0 , 86000 );
2016-10-15 00:29:55 +01:00
}
2017-11-24 03:37:52 -06:00
echo 'Refreshing alert rules queries' . PHP_EOL ;
$rules = dbFetchRows ( 'SELECT `id`, `rule` FROM `alert_rules`' );
foreach ( $rules as $rule ) {
$data [ 'query' ] = GenSQL ( $rule [ 'rule' ]);
if ( ! empty ( $data [ 'query' ])) {
dbUpdate ( $data , 'alert_rules' , 'id=?' , array ( $rule [ 'id' ]));
unset ( $data );
}
}
} catch ( LockException $e ) {
echo $e -> getMessage () . PHP_EOL ;
exit ( - 1 );
2016-10-15 00:29:55 +01:00
}
}
2016-12-12 14:26:17 +00:00
if ( $options [ 'f' ] === 'notify' ) {
if ( isset ( $config [ 'alert' ][ 'default_mail' ])) {
send_mail (
$config [ 'alert' ][ 'default_mail' ],
'[LibreNMS] Auto update has failed' ,
" We just attempted to update your install but failed. The information below should help you fix this. \r \n \r \n " . $options [ 'o' ]
);
}
}
2017-03-22 10:17:13 +00:00
if ( $options [ 'f' ] === 'peeringdb' ) {
2017-11-24 03:37:52 -06:00
try {
if ( Config :: get ( 'distributed_poller' )) {
MemcacheLock :: lock ( 'peeringdb' , 0 , 86000 );
}
cache_peeringdb ();
} catch ( LockException $e ) {
echo $e -> getMessage () . PHP_EOL ;
exit ( - 1 );
}
2017-03-22 10:17:13 +00:00
}
2017-10-27 15:03:58 -05:00
if ( $options [ 'f' ] === 'refresh_os_cache' ) {
2017-11-03 16:10:24 -05:00
echo 'Clearing OS cache' . PHP_EOL ;
unlink ( Config :: get ( 'install_dir' ) . '/cache/os_defs.cache' );
2017-10-27 15:03:58 -05:00
}