Skip to content

Commit

Permalink
Task Scheduler Configuration (librenms#16356)
Browse files Browse the repository at this point in the history
* Removed unneeded config options and added new options for tasks

* Move update config back to the service daemon section

* Update billing tasks to use new config variables

* Fixed binlling service check and added discovery tests

* Added order to new config options

* Lint fixes

* Added -f option to billing PHP scripts to allow them to be forced to run from the scheduler

* Fixed discovery php script to not run without a device spec if the cron scheduler isn't enabled

* Update alerting to use the new config option

* Updated fast ping to use new option

* Revert discovery php changes (it is not called directly from cron)

* Update services to use the new config option

* Update the poller to use the new config options

* Remove comment

* Documentation updates

* Updated the "legacy" option name to "default"

* Formatting

* Fix documentation

* Fix for a remaining reference to the "legacy" config option

* Typo fixes and better error messages

* Update Services.md

* Update the default scheduler type to "unrestricted"

* Update Dispatcher-Service.md

* Update all code and docs to use the name dispatcherservice

* Rename various items for consistency

* Remove legacy translated strings

* Fix up poller cluster global setting logic

* Change back to legacy :D

* Add block to poller.php

* Fix wrong "config" in alerts.php

* Add block for ping.php
It is indeed possible to configure ping.php via the cron

* Update help text

* Move option descriptions to translations file

* Correction

* Fixed type in poller cluster model

* Remove LibreNMS scheduler option for poller because it's still a work in progress

* Fixed type in poller cluster model

---------

Co-authored-by: Tony Murray <[email protected]>
  • Loading branch information
eskyuu and murrant authored Nov 12, 2024
1 parent 46d0dab commit 1110e8c
Show file tree
Hide file tree
Showing 19 changed files with 301 additions and 218 deletions.
6 changes: 3 additions & 3 deletions LibreNMS/queuemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,11 @@ def do_dispatch(self):
def do_work(self, run_type, group):
if run_type == "poll":
logger.info("Polling billing")
args = ("-d") if self.config.debug else ()
args = ("-d", "-f") if self.config.debug else ("-f")
exit_code, output = LibreNMS.call_script("poll-billing.php", args)
else: # run_type == 'calculate'
logger.info("Calculating billing")
args = ("-d") if self.config.debug else ()
args = ("-d", "-f") if self.config.debug else ("-f")
exit_code, output = LibreNMS.call_script("billing-calculate.php", args)

if exit_code != 0:
Expand Down Expand Up @@ -506,7 +506,7 @@ def do_dispatch(self):

def do_work(self, device_id, group):
logger.info("Checking alerts")
args = ("-d") if self.config.debug else ()
args = ("-d", "-f") if self.config.debug else ("-f")
exit_code, output = LibreNMS.call_script("alerts.php", args)

if self.config.log_output:
Expand Down
39 changes: 31 additions & 8 deletions LibreNMS/service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import logging
import os
import pymysql # pylint: disable=import-error
import sys
import threading
import time

import pymysql # pylint: disable=import-error

import LibreNMS
from LibreNMS.config import DBConfig

Expand Down Expand Up @@ -136,39 +135,63 @@ def populate(self):
self.log_level = config.get("poller_service_loglevel", ServiceConfig.log_level)

# new options
self.poller.enabled = config.get("service_poller_enabled", True) # unused
self.poller.enabled = (
config.get("service_poller_enabled", True)
if config.get("schedule_type").get("poller", "legacy") == "legacy"
else config.get("schedule_type").get("poller", "legacy") == "dispatcher"
)
self.poller.workers = config.get(
"service_poller_workers", ServiceConfig.poller.workers
)
self.poller.frequency = config.get(
"service_poller_frequency", ServiceConfig.poller.frequency
)
self.discovery.enabled = config.get("service_discovery_enabled", True) # unused
self.discovery.enabled = (
config.get("service_discovery_enabled", True)
if config.get("schedule_type").get("discovery", "legacy") == "legacy"
else config.get("schedule_type").get("discovery", "legacy") == "dispatcher"
)
self.discovery.workers = config.get(
"service_discovery_workers", ServiceConfig.discovery.workers
)
self.discovery.frequency = config.get(
"service_discovery_frequency", ServiceConfig.discovery.frequency
)
self.services.enabled = config.get("service_services_enabled", True)
self.services.enabled = (
config.get("service_services_enabled", True)
if config.get("schedule_type").get("services", "legacy") == "legacy"
else config.get("schedule_type").get("services", "legacy") == "dispatcher"
)
self.services.workers = config.get(
"service_services_workers", ServiceConfig.services.workers
)
self.services.frequency = config.get(
"service_services_frequency", ServiceConfig.services.frequency
)
self.billing.enabled = config.get("service_billing_enabled", True)
self.billing.enabled = (
config.get("service_billing_enabled", True)
if config.get("schedule_type").get("billing", "legacy") == "legacy"
else config.get("schedule_type").get("billing", "legacy") == "dispatcher"
)
self.billing.frequency = config.get(
"service_billing_frequency", ServiceConfig.billing.frequency
)
self.billing.calculate = config.get(
"service_billing_calculate_frequency", ServiceConfig.billing.calculate
)
self.alerting.enabled = config.get("service_alerting_enabled", True)
self.alerting.enabled = (
config.get("service_alerting_enabled", True)
if config.get("schedule_type").get("alerting", "legacy") == "legacy"
else config.get("schedule_type").get("alerting", "legacy") == "dispatcher"
)
self.alerting.frequency = config.get(
"service_alerting_frequency", ServiceConfig.alerting.frequency
)
self.ping.enabled = config.get("service_ping_enabled", False)
self.ping.enabled = (
config.get("service_ping_enabled", False)
if config.get("schedule_type").get("ping", "legacy") == "legacy"
else config.get("schedule_type").get("ping", "legacy") == "dispatcher"
)
self.ping.frequency = config.get("ping_rrd_step", ServiceConfig.ping.frequency)
self.down_retry = config.get(
"service_poller_down_retry", ServiceConfig.down_retry
Expand Down
10 changes: 9 additions & 1 deletion alerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@
$init_modules = ['alerts', 'laravel'];
require __DIR__ . '/includes/init.php';

$options = getopt('d::');
$options = getopt('fd::');

if (Debug::set(isset($options['d']))) {
echo "DEBUG!\n";
}

$scheduler = \LibreNMS\Config::get('schedule_type.alerting');
if (! isset($options['f']) && $scheduler != 'legacy' && $scheduler != 'cron') {
if (Debug::isEnabled()) {
echo "Alerts are not enabled for cron scheduling. Add the -f command argument if you want to force this command to run.\n";
}
exit(0);
}

$alerts_lock = Cache::lock('alerts', \LibreNMS\Config::get('service_alerting_frequency'));
if ($alerts_lock->get()) {
$alerts = new RunAlerts();
Expand Down
33 changes: 21 additions & 12 deletions app/Models/PollerCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ public function configDefinition($groups = null)
$groups = PollerGroup::list();
}

$scheduleType = \LibreNMS\Config::get('schedule_type');

$pollerGloballyEnabled = $scheduleType['poller'] == 'legacy' ? \LibreNMS\Config::get('service_poller_enabled', true) : $scheduleType['poller'] == 'dispatcher';
$discoveryGloballyEnabled = $scheduleType['discovery'] == 'legacy' ? \LibreNMS\Config::get('service_discovery_enabled', true) : $scheduleType['discovery'] == 'dispatcher';
$servicesGloballyEnabled = $scheduleType['services'] == 'legacy' ? \LibreNMS\Config::get('service_services_enabled', true) : $scheduleType['services'] == 'dispatcher';
$alertGloballyEnabled = $scheduleType['alerting'] == 'legacy' ? \LibreNMS\Config::get('service_alerting_enabled', true) : $scheduleType['alerting'] == 'dispatcher';
$billingGloballyEnabled = $scheduleType['billing'] == 'legacy' ? \LibreNMS\Config::get('service_billing_enabled', true) : $scheduleType['billing'] == 'dispatcher';
$pingGloballyEnabled = $scheduleType['ping'] == 'legacy' ? \LibreNMS\Config::get('service_ping_enabled', true) : $scheduleType['ping'] == 'dispatcher';

return [
[
'name' => 'poller_groups',
Expand All @@ -112,8 +121,8 @@ public function configDefinition($groups = null)
],
[
'name' => 'poller_enabled',
'default' => \LibreNMS\Config::get('service_poller_enabled'),
'value' => (bool) ($this->poller_enabled ?? \LibreNMS\Config::get('service_poller_enabled')),
'default' => $pollerGloballyEnabled,
'value' => (bool) ($this->poller_enabled ?? $pollerGloballyEnabled),
'type' => 'boolean',
],
[
Expand All @@ -140,8 +149,8 @@ public function configDefinition($groups = null)
],
[
'name' => 'discovery_enabled',
'default' => \LibreNMS\Config::get('service_discovery_enabled'),
'value' => (bool) ($this->discovery_enabled ?? \LibreNMS\Config::get('service_discovery_enabled')),
'default' => $discoveryGloballyEnabled,
'value' => (bool) ($this->discovery_enabled ?? $discoveryGloballyEnabled),
'type' => 'boolean',
],
[
Expand All @@ -161,8 +170,8 @@ public function configDefinition($groups = null)
],
[
'name' => 'services_enabled',
'default' => \LibreNMS\Config::get('service_services_enabled'),
'value' => (bool) ($this->services_enabled ?? \LibreNMS\Config::get('service_services_enabled')),
'default' => $servicesGloballyEnabled,
'value' => (bool) ($this->services_enabled ?? $servicesGloballyEnabled),
'type' => 'boolean',
],
[
Expand All @@ -182,8 +191,8 @@ public function configDefinition($groups = null)
],
[
'name' => 'billing_enabled',
'default' => \LibreNMS\Config::get('service_billing_enabled'),
'value' => (bool) ($this->billing_enabled ?? \LibreNMS\Config::get('service_billing_enabled')),
'default' => $billingGloballyEnabled,
'value' => (bool) ($this->billing_enabled ?? $billingGloballyEnabled),
'type' => 'boolean',
],
[
Expand All @@ -204,8 +213,8 @@ public function configDefinition($groups = null)
],
[
'name' => 'alerting_enabled',
'default' => \LibreNMS\Config::get('service_alerting_enabled'),
'value' => (bool) ($this->alerting_enabled ?? \LibreNMS\Config::get('service_alerting_enabled')),
'default' => $alertGloballyEnabled,
'value' => (bool) ($this->alerting_enabled ?? $alertGloballyEnabled),
'type' => 'boolean',
],
[
Expand All @@ -218,8 +227,8 @@ public function configDefinition($groups = null)
],
[
'name' => 'ping_enabled',
'default' => \LibreNMS\Config::get('service_ping_enabled'),
'value' => (bool) ($this->ping_enabled ?? \LibreNMS\Config::get('service_ping_enabled')),
'default' => $pingGloballyEnabled,
'value' => (bool) ($this->ping_enabled ?? $pingGloballyEnabled),
'type' => 'boolean',
],
[
Expand Down
15 changes: 14 additions & 1 deletion billing-calculate.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,31 @@
*/

use LibreNMS\Billing;
use LibreNMS\Config;
use LibreNMS\Util\Debug;
use LibreNMS\Util\Number;

$init_modules = [];
require __DIR__ . '/includes/init.php';

$options = getopt('r');
$options = getopt('frd');

if (isset($options['r'])) {
echo "Clearing history table.\n";
DB::table('bill_history')->truncate();
}

Debug::set(isset($options['d']));

$scheduler = Config::get('schedule_type.billing');
if (! isset($options['f']) && $scheduler != 'legacy' && $scheduler != 'cron') {
if (Debug::isEnabled()) {
echo "Billing is not enabled for cron scheduling. Add the -f command ar
gument if you want to force this command to run.\n";
}
exit(0);
}

foreach (dbFetchRows('SELECT * FROM `bills` ORDER BY `bill_id`') as $bill) {
echo str_pad($bill['bill_id'] . ' ' . $bill['bill_name'], 30) . " \n";

Expand Down
8 changes: 8 additions & 0 deletions check-services.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
$params[] = $options['h'];
}
}
} else {
$scheduler = Config::get('schedule_type.services');
if ($scheduler != 'legacy' && $scheduler != 'cron') {
if (Debug::isEnabled()) {
echo "Services are not enabled for cron scheduling\n";
}
exit(0);
}
}

$sql = 'SELECT D.*,S.*,attrib_value FROM `devices` AS D'
Expand Down
8 changes: 7 additions & 1 deletion discovery-wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
This is a Bootstrap script for wrapper.py, in order to retain compatibility with earlier LibreNMS setups
"""

import logging
import os
import sys
import logging
from argparse import ArgumentParser

import LibreNMS
Expand Down Expand Up @@ -51,6 +51,12 @@
log_file = os.path.join(log_dir, WRAPPER_TYPE + "_wrapper.log")
logger = LibreNMS.logger_get_logger(log_file, debug=args.debug)

scheduler = config.get("schedule_type").get("discovery", "legacy")
enabled = True if scheduler == "legacy" else scheduler == "cron"
if not enabled:
logger.debug("Discovery is not enabled for cron scheduling")
sys.exit(0)

try:
amount_of_workers = int(args.amount_of_workers)
except (IndexError, ValueError):
Expand Down
16 changes: 11 additions & 5 deletions doc/Extensions/Dispatcher-Service.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,25 @@ Additional configuration settings can be set in your config.
The defaults are shown here - it's recommended that you at least tune
the number of workers.

!!! setting "poller/distributed"
!!! setting "poller/dispatcherservice"
```bash
lnms config:set service_poller_workers 24
lnms config:set service_services_workers 8
lnms config:set service_discovery_workers 16
lnms config:set schedule_type.poller dispatcher
lnms config:set schedule_type.services dispatcher
lnms config:set schedule_type.discovery dispatcher
```

Optional Settings

!!! setting "poller/distributed"
!!! setting "poller/dispatcherservice"
```bash
lnms config:set service_poller_frequency 300
lnms config:set service_services_frequency 300
lnms config:set service_discovery_frequency 21600
lnms config:set schedule_type.alert dispatcher
lnms config:set schedule_type.billing dispatcher
lnms config:set service_billing_frequency 300
lnms config:set service_billing_calculate_frequency 60
lnms config:set service_poller_down_retry 60
Expand Down Expand Up @@ -166,9 +171,10 @@ Maximum WS equals the number of workers multiplied with the number of seconds in
The [fast ping](Fast-Ping-Check.md) scheduler is disabled by default.
You can enable it by setting the following:

```php
$config['service_ping_enabled'] = true;
```
!!! setting "poller/scheduledtasks"
```bash
lnms config:set schedule_type.ping dispatcher
```

## Watchdog

Expand Down
41 changes: 11 additions & 30 deletions doc/Extensions/Services.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,36 +209,17 @@ then you can run the following command to help troubleshoot services.
These settings are related and should be investigated and set accordingly.
The below values are not defaults or recommended.

```php
$config['service_poller_enabled'] = true;
```
```php
$config['service_poller_workers'] = 16;
```
```php
$config['service_poller_frequency'] = 300;
```
```php
$config['service_poller_down_retry'] = 5;
```
```php
$config['service_discovery_enabled'] = true;
```
```php
$config['service_discovery_workers'] = 16;
```
```php
$config['service_discovery_frequency'] = 3600;
```
```php
$config['service_services_enabled'] = true;
```
```php
$config['service_services_workers'] = 16;
```
```php
$config['service_services_frequency'] = 60;
```
!!! setting "poller/scheduledtasks"
```bash
lnms config:set schedule_type.services dispatcher
```

!!! setting "poller/dispatcherservice"
```bash
lnms config:set service_services_workers 16
lnms config:set service_discovery_workers 300
```
Please also see [Dispatcher Service](../Extensions/Dispatcher-Service.md)

### Service checks polling logic

Expand Down
Loading

0 comments on commit 1110e8c

Please sign in to comment.