2016-08-24 08:12:20 +01:00
source: Extensions/Component.md
2018-10-27 23:04:34 +01:00
path: blob/master/doc/
2017-08-21 13:49:07 -04:00
# About
2015-08-15 16:01:43 +10:00
The Component extension provides a generic database storage mechanism for discovery and poller modules.
The Driver behind this extension was to provide the features of ports, in a generic manner to discovery/poller modules.
2016-03-10 17:30:32 +10:00
It provides a status (Nagios convention), the ability to Disable (do not poll), or Ignore (do not Alert).
2015-08-15 16:01:43 +10:00
2017-08-21 13:49:07 -04:00
# Database Structure
2015-08-15 16:01:43 +10:00
The database structure contains the component table:
```SQL
mysql> select * from component limit 1;
+----+-----------+------+------------+--------+----------+--------+-------+
| id | device_id | type | label | status | disabled | ignore | error |
+----+-----------+------+------------+--------+----------+--------+-------+
| 9 | 1 | TEST | TEST LABEL | 0 | 1 | 1 | |
+----+-----------+------+------------+--------+----------+--------+-------+
1 row in set (0.00 sec)
```
These fields are described below:
2017-08-21 13:49:07 -04:00
- `id` - ID for each component, unique index
- `device_id` - device_id from the devices table
- `type` - name from the component_type table
- `label` - Display label for the component
- `status` - The status of the component, retrieved from the device
- `disabled` - Should this component be polled?
- `ignore` - Should this component be alerted on
- `error` - Error message if in Alert state
2015-08-15 16:01:43 +10:00
The component_prefs table holds custom data in an Attribute/Value format:
2017-08-21 13:49:07 -04:00
```sql
2015-08-15 16:01:43 +10:00
mysql> select * from component_prefs limit 1;
+----+-----------+-----------+-----------+
| id | component | attribute | value |
+----+-----------+-----------+-----------+
| 4 | 9 | TEST_ATTR | TEST_ATTR |
+----+-----------+-----------+-----------+
2 rows in set (0.00 sec)
```
2017-08-21 13:49:07 -04:00
## Reserved Fields
2015-08-15 16:01:43 +10:00
2017-08-21 13:49:07 -04:00
When this data from both the `component` and `component_prefs` tables is returned in one single consolidated array, there is the potential for someone to attempt to set an attribute (in the `component_prefs` ) table that is used in the `component` table.
Because of this all fields of the `component` table are reserved, they cannot be used as custom attributes, if you update these the module will attempt to write them to the `component` table, not the `component_prefs` table.
2015-08-15 16:01:43 +10:00
2017-08-21 13:49:07 -04:00
# Using Components
2015-08-15 16:01:43 +10:00
2016-08-21 08:07:14 -05:00
Create an instance of the component class:
2015-08-15 16:01:43 +10:00
```php
2016-08-21 08:07:14 -05:00
$COMPONENT = new LibreNMS\Component();
2015-08-15 16:01:43 +10:00
```
2017-08-21 13:49:07 -04:00
## Retrieving Components
2015-08-15 16:01:43 +10:00
Now you can retrieve an array of the available components:
```php
2017-08-21 13:49:07 -04:00
$ARRAY = $COMPONENT->getComponents($DEVICE_ID, $OPTIONS);
2015-08-15 16:01:43 +10:00
```
`getComponents` takes 2 arguments:
2017-08-21 13:49:07 -04:00
- `DEVICE_ID` or null for all devices.
- `OPTIONS` - an array of various options.
2015-08-15 16:01:43 +10:00
`getComponents` will return an array containing components in the following format:
2017-08-21 13:49:07 -04:00
```php
Array
(
[X] => Array
2015-08-15 16:01:43 +10:00
(
2017-08-21 13:49:07 -04:00
[Y1] => Array
(
[device_id] => 1
[TEST_ATTR] => TEST_ATTR
[type] => TEST
[label] => TEST LABEL
[status] => 0
[ignore] => 1
[disabled] => 1
[error] =>
),
[Y2] => Array
2015-08-15 16:01:43 +10:00
(
2017-08-21 13:49:07 -04:00
[device_id] => 1
[TEST_ATTR] => TEST_ATTR
[type] => TESTING
[label] => TEST LABEL
[status] => 0
[ignore] => 1
[disabled] => 0
[error] =>
),
2015-08-15 16:01:43 +10:00
)
2017-08-21 13:49:07 -04:00
)
```
2015-08-15 16:01:43 +10:00
2017-08-21 13:49:07 -04:00
Where X is the Device ID and Y1/Y2 is the Component ID. In the example above, `TEST_ATTR` is a custom field, the rest are reserved fields.
### Options
2015-08-15 16:01:43 +10:00
Options can be supplied to `getComponents` to influence which and how components are returned.
2017-08-21 13:49:07 -04:00
#### Filtering
2015-08-15 16:01:43 +10:00
You can filter on any of the [reserved ](#reserved ) fields. Filters are created in the following format:
```php
2016-08-21 08:07:14 -05:00
$options['filter']['FIELD'] = array ('OPERATOR', 'CRITERIA');
2015-08-15 16:01:43 +10:00
```
Where:
2017-08-21 13:49:07 -04:00
- `FIELD` - The [reserved ](#reserved ) field to filter on
- `OPERATOR` - 'LIKE' or '=', are we checking if the FIELD equals or contains the CRITERIA.
- `CRITERIA` - The criteria to search on
2015-08-15 16:01:43 +10:00
There are 2 filtering shortcuts:
2016-01-30 21:06:58 -05:00
2017-08-21 13:49:07 -04:00
`$DEVICE_ID` is a synonym for:
2015-08-15 16:01:43 +10:00
```php
$OPTIONS['filter']['device_id'] = array ('=', $DEVICE_ID);
```
`$OPTIONS['type'] = $TYPE` is a synonym for:
```php
$OPTIONS['filter']['type'] = array ('=', $TYPE);
```
2017-08-21 13:49:07 -04:00
#### Sorting
2015-08-15 16:01:43 +10:00
You can sort the records that are returned by specifying the following option:
```php
$OPTIONS['sort'][FIELD] = 'DIRECTION';
```
Where Direction is one of:
2017-08-21 13:49:07 -04:00
- `ASC` - Ascending, from Low to High
- `DESC` - Descending, from High to Low
2015-08-15 16:01:43 +10:00
2017-08-21 13:49:07 -04:00
## Creating Components
2015-08-15 16:01:43 +10:00
2017-08-21 13:49:07 -04:00
To create a new component, run the `createComponent` function.
2015-08-15 16:01:43 +10:00
```php
2017-08-21 13:49:07 -04:00
$ARRAY = $COMPONENT->createComponent($DEVICE_ID, $TYPE);
2015-08-15 16:01:43 +10:00
```
`createComponent` takes 2 arguments:
2017-08-21 13:49:07 -04:00
- `DEVICE_ID` - The ID of the device to attach the component to.
- `TYPE` - The unique type for your module.
2015-08-15 16:01:43 +10:00
This will return a new, empty array with a component ID and Type set, all other fields will be set to defaults.
2017-08-21 13:49:07 -04:00
```php
Array
(
[1] => Array
2015-08-15 16:01:43 +10:00
(
2017-08-21 13:49:07 -04:00
[type] => TESTING
[label] =>
[status] => 1
[ignore] => 0
[disabled] => 0
[error] =>
2015-08-15 16:01:43 +10:00
)
2017-08-21 13:49:07 -04:00
)
```
2015-08-15 16:01:43 +10:00
2017-08-21 13:49:07 -04:00
## Deleting Components
2015-08-15 16:01:43 +10:00
When a component is no longer needed, it can be deleted.
```php
$COMPONENT->deleteComponent($COMPONENT_ID)
```
2017-08-21 13:49:07 -04:00
This will return `True` on success or `False` on failure.
2015-08-15 16:01:43 +10:00
2017-08-21 13:49:07 -04:00
## Editing Components
2015-08-15 16:01:43 +10:00
To edit a component, the procedure is:
1. [Get the Current Components ](#get )
2. [Edit the array ](#update-edit )
3. [Write the components ](#update-write )
2017-08-21 13:49:07 -04:00
### Edit the Array
2015-08-15 16:01:43 +10:00
2017-08-21 13:49:07 -04:00
Once you have a component array from `getComponents` the first thing to do is extract the components for only the single device you are editing. This is required because the `setComponentPrefs` function only saves a single device at a time.
2015-08-15 16:01:43 +10:00
```php
2017-08-21 13:49:07 -04:00
$ARRAY = $COMPONENT->getComponents($DEVICE_ID, $OPTIONS);
2015-08-15 16:01:43 +10:00
$ARRAY = $ARRAY[$DEVICE_ID];
```
Then simply edit this array to suit your needs.
If you need to add a new Attribute/Value pair you can:
```php
2017-08-21 13:49:07 -04:00
$ARRAY[COMPONENT_ID]['New Attribute'] = "Value";
2015-08-15 16:01:43 +10:00
```
If you need to delete a previously set Attribute/Value pair you can:
```php
unset($ARRAY[COMPONENT_ID]['New Attribute']);
```
If you need to edit a previously set Attribute/Value pair you can:
```php
$ARRAY[COMPONENT_ID]['Existing Attribute'] = "New Value";
```
2017-08-21 13:49:07 -04:00
### Write the components
2015-08-15 16:01:43 +10:00
To write component changes back to the database simply:
```php
2017-08-21 13:49:07 -04:00
$COMPONENT->setComponentPrefs($DEVICE_ID, $ARRAY)
2015-08-15 16:01:43 +10:00
```
When writing the component array there are several caveats to be aware of, these are:
2017-08-21 13:49:07 -04:00
- `$ARRAY` must be in the format of a single device ID - `$ARRAY[$COMPONENT_ID][Attribute] = 'Value';` NOT in the multi device format returned by `getComponents` - `$ARRAY[$DEVICE_ID][$COMPONENT_ID][Attribute] = 'Value';`
2015-08-15 16:01:43 +10:00
- You cannot edit the Component ID or the Device ID
- [reserved ](#reserved ) fields can not be removed
- if a change is found an entry will be written to the eventlog.
2017-08-21 13:49:07 -04:00
## API
2015-08-15 16:01:43 +10:00
Component details are available via the API.
2017-08-21 13:49:07 -04:00
Please see the [API-Docs ](/API/#function-get_components ) for details.
2015-08-15 16:01:43 +10:00
2017-08-21 13:49:07 -04:00
## Alerting
2015-08-15 16:01:43 +10:00
2015-12-22 11:05:42 +10:00
It is intended that discovery/poller modules will detect the status of a component during the polling cycle.
2016-03-10 17:30:32 +10:00
Status is logged using the Nagios convention for status codes, where:
0 = Ok,
1 = Warning,
2 = Critical
2015-12-22 11:05:42 +10:00
2016-03-10 17:30:32 +10:00
If you are creating a poller module which can detect a fault condition simply set STATUS to something other than 0 and ERROR to a message that indicates the problem.
2015-12-22 11:05:42 +10:00
To actually raise an alert, the user will need to create an alert rule. To assist with this several Alerting Macro's have been created:
2017-08-21 13:49:07 -04:00
- `%macro.component_normal` - A component that is not disabled or ignored and in a Normal state.
- `%macro.component_warning` - A component that is not disabled or ignored and NOT in a Warning state.
- `%macro.component_critical` - A component that is not disabled or ignored and NOT in a Critical state.
2015-12-22 11:05:42 +10:00
To raise alerts for components, the following rules could be created:
2017-08-21 13:49:07 -04:00
- `%macros.component_critical = "1"` - To alert on all Critical components
- `%macros.component_critical = "1" && %component.type = "<Type of Component>"` - To alert on all Critical components of a particular type.
2015-12-22 11:05:42 +10:00
If there is a particular component you would like excluded from alerting, simply set the ignore field to 1.
2015-08-15 16:01:43 +10:00
The data that is written to each alert when it is raised is in the following format:
`COMPONENT_TYPE - LABEL - ERROR`
2017-08-21 13:49:07 -04:00
# Example Code
2015-08-15 16:01:43 +10:00
To see an example of how the component module can used, please see the following modules:
- Cisco CBQoS
2017-08-21 13:49:07 -04:00
- `includes/discovery/cisco-cbqos.inc.php`
- `includes/polling/cisco-cbqos.inc.php`
- `html/includes/graphs/device/cbqos_traffic.inc.php`
2015-08-15 16:01:43 +10:00
- Cisco OTV
2017-08-21 13:49:07 -04:00
- `includes/discovery/cisco-otv.inc.php`
- `includes/polling/cisco-otv.inc.php`
- `html/includes/graphs/device/cisco-otv-mac.inc.php`
- `html/pages/routing/cisco-otv.inc.php`