Skip to content

Commit

Permalink
Cli parse implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
BausPhi committed Oct 5, 2021
1 parent 4250199 commit 9df39f0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
venv/
.idea/
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# check_ripe_db
Monitoring check plugin that queries the RIPE database and checks whether its results match the expectations
Monitoring check plugin that queries the RIPE database and checks whether its results match the expectations.

### Usage
Possible options:

| Option | Description | required? |
|-----------------------------------------------------------------|----------------------------|---------------------|
| -h | Help | no |
| -s/--source <DB source>: | The DB source to query | no (default: ripe) |
| -o/--objecttype <DB objecttype> | The DB objecttype to query | yes |
| -k/--key <DB search key> | The DB key to query | yes |
| -e/--expected "<(attribute, [value, value2], match_mode), ...>" | The expected attributes | yes |

There are three different match modes for the expected values:

1. SINGLEVALUE: The expected value has to match the resulting value
2. EXACTLIST: The expacted values must exactly match the resulting values list

A possible usage could look like this:

```shell
python check_ripe_db.py -s "ripe" -o "aut-num" -k "as44163" -e "(status, Assigned, SINGLEVALUE), (source, [Filtered, Assigned], EXACTLIST)"
```

### Dependencies

The plugin only needs the requests library to run. The library can be installed by running:

```shell
python -m pip install requests
```
55 changes: 55 additions & 0 deletions check_ripe_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import argparse
import json

import requests


def parse_cli():
parser = argparse.ArgumentParser(
description="Monitoring check plugin to query the RIPE database and check if the values match the expectations")
parser.add_argument("-s", "--source", type=str, help="RIPE database source", default="ripe")
parser.add_argument("-o", "--objecttype", type=str, help="RIPE database objecttype")
parser.add_argument("-k", "--key", type=str, help="RIPE database objecttype")
parser.add_argument("-e", "--expected", type=str, help="Expected values to check")
args = parser.parse_args()
args = args.__dict__
if "objecttype" not in args.keys():
print("UNKNOWN - The DB objecttype (-o/--objecttype) is required, but was not given")
exit(3)
if "key" not in args.keys():
print("UNKNOWN - The DB key (-k/--key) is required, but was not given")
exit(3)
if "expected" not in args.keys():
print("UNKNOWN - The expected values (-e/--expected) are required, but were not given")
exit(3)
source, objecttype, key = args["source"], args["objecttype"], args["key"]
expected_string = args["expected"]
expected = []
for value in expected_string.split("),"):
elements = value.split(",")
if len(elements) < 3:
print("UNKNOWN - The expected results format is wrong, please use the following format:"
"<(attribute, [value, ...], match_mode), ...>")
exit(3)
else:
values = []
for val in elements[1:len(elements)-1]:
values.append(val.replace("[", "").replace("]", "").lstrip(" "))
expected.append((elements[0].replace("(", ""), values, elements[len(elements)-1].replace(")", "")))
return source, objecttype, key, expected


def check_values(res):
pass


def main():
src, objtype, key, exp = parse_cli()
url = f"https://rest.db.ripe.net/{src}/{objtype}/{key}"
resp = requests.get(url, headers={"Accept": "application/json"})
resp_dict = json.loads(resp.text)
check_values(resp_dict)


if __name__ == "__main__":
main()

0 comments on commit 9df39f0

Please sign in to comment.