Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ns-api: added ns.log apis #192

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/ns-api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=ns-api
PKG_VERSION:=0.0.9
PKG_VERSION:=0.0.10
PKG_RELEASE:=1

PKG_BUILD_DIR:=$(BUILD_DIR)/ns-api-$(PKG_VERSION)
Expand Down Expand Up @@ -72,6 +72,8 @@ define Package/ns-api/install
$(INSTALL_BIN) ./files/ns.firewall $(1)/usr/libexec/rpcd/
$(INSTALL_DATA) ./files/ns.firewall.json $(1)/usr/share/rpcd/acl.d/
$(INSTALL_BIN) ./files/ns.flashstart $(1)/usr/libexec/rpcd/
$(INSTALL_DATA) ./files/ns.log.json $(1)/usr/share/rpcd/acl.d/
$(INSTALL_BIN) ./files/ns.log $(1)/usr/libexec/rpcd/
$(INSTALL_DATA) ./files/ns.flashstart.json $(1)/usr/share/rpcd/acl.d/
$(INSTALL_BIN) ./files/ns.dpireport $(1)/usr/libexec/rpcd/
$(INSTALL_DATA) ./files/ns.dpireport.json $(1)/usr/share/rpcd/acl.d/
Expand Down
31 changes: 31 additions & 0 deletions packages/ns-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2352,3 +2352,34 @@ If the storage is configured, the response will be like:
"vendor": "QEMU"
}
```

## ns.log

Show and filter logs.

### get-log

```bash
api-cli ns.log get-log --data '{"limit": 10, "search: "mwan"}'
```

Parameter list:

- `limit`: number of lines to show
- `search`: search string, uses `grep` syntax

Both parameters are _optional_

Example response:

```json
{
"values": [
"Oct 12 08:56:55 NethSec dropbear[21682]: Exit (root) from <W.X.Y.Z:00000>: Disconnect received",
"Oct 12 09:00:00 NethSec crond[4002]: USER root pid 22583 cmd sleep $(( RANDOM % 60 )); /usr/sbin/send-heartbeat",
"..."
]
}
```

**Notes**: returning strings are syslog formatted, be aware of it if any parsing is needed.
53 changes: 53 additions & 0 deletions packages/ns-api/files/ns.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/python3

#
# Copyright (C) 2023 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-2.0-only
#

import json
import subprocess
import sys
from json import JSONDecodeError

from nethsec import utils

cmd = sys.argv[1]

if cmd == 'list':
print(json.dumps({
'get-log': {
'search': 'String',
'limit': 32
}
}))
elif cmd == 'call':
action = sys.argv[2]
if action == 'get-log':
try:
data = json.JSONDecoder().decode(sys.stdin.read())
# parse params
limit = data['limit'] if 'limit' in data else 100
search = data['search'] if 'search' in data else None

# search in log
if search is not None:
result = subprocess.run(['grep', search, '/var/log/messages'], capture_output=True, check=True).stdout
else:
result = subprocess.run(['cat', '/var/log/messages'], capture_output=True, check=True).stdout

result = subprocess.run(['tail', '-n', str(limit)], input=result, capture_output=True, check=True).stdout

print(json.dumps({
'values': result.decode('utf-8').split('\n')[:-1]
}))
except JSONDecodeError as ex:
print(json.dumps(utils.generic_error('Invalid JSON')))
except subprocess.CalledProcessError as ex:
decoded_error: str = ex.stderr.decode('utf-8').strip('\n')
if ex.cmd[0] == 'grep':
print(json.dumps(utils.validation_error('search', decoded_error.removesuffix('grep: '))))
elif ex.cmd[0] == 'tail':
print(json.dumps(utils.validation_error('limit', decoded_error.removesuffix('tail: '))))
else:
print(json.dumps(utils.generic_error(decoded_error)))
13 changes: 13 additions & 0 deletions packages/ns-api/files/ns.log.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"log-manager": {
"description": "View and search system logs",
"write": {},
"read": {
"ubus": {
"ns.log": [
"*"
]
}
}
}
}