Created
November 13, 2024 06:01
-
-
Save rudimuliawan/e301753880b04512338aaff2d9f474b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import ncs | |
| import requests | |
| from requests.auth import HTTPBasicAuth | |
| class Itop: | |
| url = "http://172.20.66.14:8000/webservices/rest.php" | |
| @classmethod | |
| def ticket_exists(cls, title): | |
| data = { | |
| "operation": "core/get", | |
| "class": "Incident", | |
| "key": { | |
| "title": title, | |
| }, | |
| } | |
| params = { | |
| "version": "1.0", | |
| "json_data": json.dumps(data), | |
| } | |
| request = requests.get( | |
| cls.url, | |
| params=params, | |
| auth=HTTPBasicAuth('admin', 'admin') | |
| ) | |
| objects = request.json()['objects'] | |
| return request.status_code == 200 and objects is not None and len(objects) > 0 | |
| @classmethod | |
| def sync_ticket(cls): | |
| with ncs.maapi.single_write_trans('admin', 'sync-alarm') as transaction: | |
| root = ncs.maagic.get_root(transaction) | |
| alarm_list = root.al__alarms.alarm_list.alarm | |
| for alarm in alarm_list: | |
| title = f'{alarm.device} - {alarm.type}' | |
| if alarm.is_cleared: | |
| data = { | |
| "operation": "core/update", | |
| "class": "Incident", | |
| "key": { | |
| "title": title, | |
| }, | |
| "fields": { | |
| "status": "Resolved", | |
| "description": alarm.last_alarm_text, | |
| }, | |
| "comment": "Update", | |
| } | |
| params = { | |
| "version": "1.0", | |
| "json_data": json.dumps(data), | |
| } | |
| request = requests.post( | |
| cls.url, | |
| params=params, | |
| auth=HTTPBasicAuth('admin', 'admin') | |
| ) | |
| else: | |
| data = { | |
| "operation": "core/create", | |
| "class": "Incident", | |
| "fields": { | |
| "title": title, | |
| "description": alarm.last_alarm_text, | |
| "org_id": "SELECT Organization WHERE name = \"Demo\"" | |
| }, | |
| "comment": "Created from NSO Action API", | |
| "output_fields": "id" | |
| } | |
| params = { | |
| "version": "1.0", | |
| "json_data": json.dumps(data), | |
| } | |
| request = requests.get( | |
| cls.url, | |
| params=params, | |
| auth=HTTPBasicAuth('admin', 'admin') | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment