| 
									
										
										
										
											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 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-14 10:53:04 -05:00
										 |  |  | chdir(__DIR__); // cwd to the directory containing this script
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | require 'includes/defaults.inc.php'; | 
					
						
							|  |  |  | require 'config.php'; | 
					
						
							|  |  |  | require_once 'includes/definitions.inc.php'; | 
					
						
							|  |  |  | require 'includes/functions.php'; | 
					
						
							| 
									
										
										
										
											2013-11-03 22:16:01 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-11 14:58:40 -07:00
										 |  |  | $options = getopt('f:d'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-30 16:57:09 +01:00
										 |  |  | if ($options['f'] === 'rrd_purge') { | 
					
						
							| 
									
										
										
										
											2016-07-31 23:39:45 +01:00
										 |  |  |     if (is_numeric($config['rrd_purge']) && $config['rrd_purge'] > 0) { | 
					
						
							| 
									
										
										
										
											2016-08-01 13:24:25 -05:00
										 |  |  |         $cmd = "find ".$config['rrd_dir']." -type f -mtime +".$config['rrd_purge']." -print -exec rm -f {} +"; | 
					
						
							| 
									
										
										
										
											2016-07-30 16:57:09 +01:00
										 |  |  |         $purge = `$cmd`; | 
					
						
							| 
									
										
										
										
											2016-08-01 15:08:57 +01:00
										 |  |  |         if (!empty($purge)) { | 
					
						
							| 
									
										
										
										
											2016-08-01 13:24:25 -05:00
										 |  |  |             echo "Purged the following RRD files due to old age (over ".$config['rrd_purge']." days old):\n"; | 
					
						
							| 
									
										
										
										
											2016-08-01 15:08:57 +01:00
										 |  |  |             echo $purge; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-07-30 16:57:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-25 08:40:42 +10:00
										 |  |  | if ($options['f'] === 'syslog') { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (is_numeric($config['syslog_purge'])) { | 
					
						
							|  |  |  |         $rows = dbFetchRow('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; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (dbDelete('syslog', 'seq >= ? AND seq < ? AND timestamp < DATE_SUB(NOW(), INTERVAL ? DAY)', array($rows, $limit, $config['syslog_purge'])) > 0) { | 
					
						
							|  |  |  |                 $rows = $limit; | 
					
						
							|  |  |  |                 echo 'Syslog cleared for entries over '.$config['syslog_purge']." days 1000 limit\n"; | 
					
						
							| 
									
										
										
										
											2016-08-28 17:32:55 -05:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dbDelete('syslog', 'seq >= ? AND timestamp < DATE_SUB(NOW(), INTERVAL ? DAY)', array($rows, $config['syslog_purge'])); | 
					
						
							| 
									
										
										
										
											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') { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (is_numeric($config['eventlog_purge'])) { | 
					
						
							|  |  |  |         if (dbDelete('eventlog', 'datetime < DATE_SUB(NOW(), INTERVAL ? DAY)', array($config['eventlog_purge']))) { | 
					
						
							|  |  |  |             echo 'Eventlog cleared for entries over '.$config['eventlog_purge']." days\n"; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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') { | 
					
						
							|  |  |  |     if (is_numeric($config['authlog_purge'])) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         if (dbDelete('authlog', 'datetime < DATE_SUB(NOW(), INTERVAL ? DAY)', array($config['authlog_purge']))) { | 
					
						
							|  |  |  |             echo 'Authlog cleared for entries over '.$config['authlog_purge']." days\n"; | 
					
						
							| 
									
										
										
										
											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') { | 
					
						
							|  |  |  |     if (is_numeric($config['perf_times_purge'])) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         if (dbDelete('perf_times', 'start < UNIX_TIMESTAMP(DATE_SUB(NOW(),INTERVAL ? DAY))', array($config['perf_times_purge']))) { | 
					
						
							|  |  |  |             echo 'Performance poller times cleared for entries over '.$config['perf_times_purge']." days\n"; | 
					
						
							| 
									
										
										
										
											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') { | 
					
						
							|  |  |  |     if (is_numeric($config['device_perf_purge'])) { | 
					
						
							| 
									
										
										
										
											2016-01-11 14:58:40 -07:00
										 |  |  |         if (dbDelete('device_perf', 'timestamp < DATE_SUB(NOW(),INTERVAL ? DAY)', array($config['device_perf_purge']))) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             echo 'Device performance times cleared for entries over '.$config['device_perf_purge']." days\n"; | 
					
						
							| 
									
										
										
										
											2015-06-22 21:55:31 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-27 15:35:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if ($options['f'] === 'notifications') { | 
					
						
							| 
									
										
										
										
											2016-01-19 21:50:00 +00:00
										 |  |  |     include_once 'includes/notifications.php'; | 
					
						
							| 
									
										
										
										
											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') { | 
					
						
							|  |  |  |     if (is_numeric($config['billing_data_purge']) && $config['billing_data_purge'] > 0) { | 
					
						
							|  |  |  |         # Deletes data older than XX months before the start of the last complete billing period
 | 
					
						
							|  |  |  |         $months = $config['billing_data_purge']; | 
					
						
							|  |  |  |         echo "Deleting billing data more than $months month before the last completed billing cycle\n"; | 
					
						
							|  |  |  |         $sql = "DELETE bill_data
 | 
					
						
							|  |  |  |                 FROM bill_data | 
					
						
							|  |  |  |                     INNER JOIN (SELECT bill_id,  | 
					
						
							|  |  |  |                         SUBDATE( | 
					
						
							|  |  |  |                             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($months)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-02 19:06:51 +00:00
										 |  |  | if ($options['f'] === 'alert_log') { | 
					
						
							|  |  |  |     if (is_numeric($config['alert_log_purge']) && $config['alert_log_purge'] > 0) { | 
					
						
							|  |  |  |         if (dbDelete('alert_log', 'time_logged < DATE_SUB(NOW(),INTERVAL ? DAY)', array($config['alert_log_purge']))) { | 
					
						
							|  |  |  |             echo 'Alert log data cleared for entries over '.$config['alert_log_purge']." days\n"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-13 16:16:39 +00:00
										 |  |  | if ($options['f'] === 'purgeusers') { | 
					
						
							|  |  |  |     $purge = 0; | 
					
						
							|  |  |  |     if (is_numeric($config['radius']['users_purge']) && $config['auth_mechanism'] === 'radius') { | 
					
						
							|  |  |  |         $purge = $config['radius']['users_purge']; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-01-17 15:26:35 +00: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
										 |  |  |     if ($purge > 0) { | 
					
						
							|  |  |  |         foreach (dbFetchRows("SELECT DISTINCT(`user`) FROM `authlog` WHERE `datetime` >= DATE_SUB(NOW(), INTERVAL ? DAY)", array($purge)) as $user) { | 
					
						
							|  |  |  |             $users[] = $user['user']; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-08-28 17:32:55 -05:00
										 |  |  |         $del_users = '"'.implode('","', $users).'"'; | 
					
						
							|  |  |  |         if (dbDelete('users', "username NOT IN ($del_users)", array($del_users))) { | 
					
						
							| 
									
										
										
										
											2015-12-13 16:16:39 +00:00
										 |  |  |             echo "Removed users that haven't logged in for $purge days"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |