mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Added inventory endpoint to API
This commit is contained in:
@@ -31,7 +31,8 @@
|
||||
- [`list_alert_rules`](#api-route-17)
|
||||
- [`add_rule`](#api-route-18)
|
||||
- [`edit_rule`](#api-route-19)
|
||||
|
||||
- [`inventory`](#api-inventory)
|
||||
- [`get_inventory`](#api-route-20)
|
||||
Describes the API structure.
|
||||
|
||||
# <a name="api-structure">`Structure`</a> [`top`](#top)
|
||||
@@ -725,3 +726,55 @@ rules
|
||||
"err-msg": ""
|
||||
}
|
||||
```
|
||||
|
||||
## <a name="api-inventory">`Inventory`</a> [`top`](#top)
|
||||
|
||||
### <a name="api-route-20">Function: `get_inventory`</a> [`top`](#top)
|
||||
|
||||
Retrieve the inventory for a device. If you call this without any parameters then you will only get part of the inventory. This is because a lot of devices nest each component, for instance you may initially have the chassis, within this the ports - 1 being an sfp cage, then the sfp itself. The way this API call is designed is to enable a recursive lookup. The first call will retrieve the root entry, included within this response will be entPhysicalIndex, you can then call for entPhysicalContainedIn which will then return the next layer of results.
|
||||
|
||||
Route: /api/v0/inventory/:hostname
|
||||
|
||||
- hostname can be either the device hostname or the device id
|
||||
|
||||
Input:
|
||||
|
||||
- entPhysicalClass: This is used to restrict the class of the inventory, for example you can specify chassis to only return items in the inventory that are labelled as chassis.
|
||||
- entPhysicalContainedIn: This is used to retrieve items within the inventory assigned to a previous component, for example specifying the chassis (entPhysicalIndex) will retrieve all items where the chassis is the parent.
|
||||
|
||||
Example:
|
||||
```curl
|
||||
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/inventory/localhost?entPhysicalContainedIn=65536
|
||||
```
|
||||
|
||||
Output:
|
||||
```text
|
||||
{
|
||||
"status": "ok",
|
||||
"err-msg": "",
|
||||
"count": 1,
|
||||
"inventory": [
|
||||
{
|
||||
"entPhysical_id": "2",
|
||||
"device_id": "32",
|
||||
"entPhysicalIndex": "262145",
|
||||
"entPhysicalDescr": "Linux 3.3.5 ehci_hcd RB400 EHCI",
|
||||
"entPhysicalClass": "unknown",
|
||||
"entPhysicalName": "1:1",
|
||||
"entPhysicalHardwareRev": "",
|
||||
"entPhysicalFirmwareRev": "",
|
||||
"entPhysicalSoftwareRev": "",
|
||||
"entPhysicalAlias": "",
|
||||
"entPhysicalAssetID": "",
|
||||
"entPhysicalIsFRU": "false",
|
||||
"entPhysicalModelName": "0x0002",
|
||||
"entPhysicalVendorType": "zeroDotZero",
|
||||
"entPhysicalSerialNum": "rb400_usb",
|
||||
"entPhysicalContainedIn": "65536",
|
||||
"entPhysicalParentRelPos": "-1",
|
||||
"entPhysicalMfgName": "0x1d6b",
|
||||
"ifIndex": "0"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
@@ -64,6 +64,10 @@ $app->group('/api', function() use ($app) {
|
||||
$app->get('/rules', 'authToken', 'list_alert_rules')->name('list_alert_rules');//api/v0/rules
|
||||
$app->post('/rules', 'authToken', 'add_edit_rule')->name('add_rule');//api/v0/rules (json data needs to be passed)
|
||||
$app->put('/rules', 'authToken', 'add_edit_rule')->name('edit_rule');//api/v0/rules (json data needs to be passed)
|
||||
// Inventory section
|
||||
$app->group('/inventory', function() use ($app) {
|
||||
$app->get('/:hostname', 'authToken', 'get_inventory')->name('get_inventory');
|
||||
});// End Inventory
|
||||
});
|
||||
$app->get('/v0', 'authToken', 'show_endpoints');//api/v0
|
||||
});
|
||||
|
@@ -659,3 +659,40 @@ function ack_alert() {
|
||||
$app->response->headers->set('Content-Type', 'application/json');
|
||||
echo _json_encode($output);
|
||||
}
|
||||
|
||||
function get_inventory() {
|
||||
global $config;
|
||||
$app = \Slim\Slim::getInstance();
|
||||
$router = $app->router()->getCurrentRoute()->getParams();
|
||||
$status = 'error';
|
||||
$err_msg = '';
|
||||
$message = '';
|
||||
$code = 500;
|
||||
$hostname = $router['hostname'];
|
||||
// use hostname as device_id if it's all digits
|
||||
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
|
||||
$sql = '';
|
||||
$params = array();
|
||||
if (isset($_GET['entPhysicalClass']) && !empty($_GET['entPhysicalClass'])) {
|
||||
$sql .= ' AND entPhysicalClass=?';
|
||||
$params[] = mres($_GET['entPhysicalClass']);
|
||||
}
|
||||
if (isset($_GET['entPhysicalContainedIn']) && !empty($_GET['entPhysicalContainedIn'])) {
|
||||
$sql .= ' AND entPhysicalContainedIn=?';
|
||||
$params[] = mres($_GET['entPhysicalContainedIn']);
|
||||
} else {
|
||||
$sql .= ' AND entPhysicalContainedIn="0"';
|
||||
}
|
||||
if (!is_numeric($device_id)) {
|
||||
$err_msg = 'Invalid device provided';
|
||||
} else {
|
||||
$inventory = dbFetchRows("SELECT * FROM `entPhysical` WHERE 1 $sql",$params);
|
||||
$code = 200;
|
||||
$status = 'ok';
|
||||
$total_inv = count($inventory);
|
||||
}
|
||||
$output = array("status" => $status, "err-msg" => $err_msg, "count" => $total_inv, "inventory" => $inventory);
|
||||
$app->response->setStatus($code);
|
||||
$app->response->headers->set('Content-Type', 'application/json');
|
||||
echo _json_encode($output);
|
||||
}
|
||||
|
Reference in New Issue
Block a user