The New nodepingpy Python Package
2024/03/28 Leave a comment
NodePing has offered a Python library over the years to support convenient usage with our API in your Python projects. Since our current Python package, nodeping-api
, was released, Python 2 has become deprecated and Python 3 has made additions. While looking at using some of those additions and the deprecation of Python 2, we decided it would be better to rewrite a whole new package with an improved design instead of improving the old one. Some improvements include:
- Using dataclasses where convenient for data being submitted to the API
- Consistent design across the different modules
- Changed docstrings to Google Style docstrings from Sphinx style
- Typing used throughout the package
Like the nodeping-api
package, nodepingpy
provides feature parity with the NodePing API and can do the following, and more:
- List, create, update, and delete checks
- Manage contacts
- Manage contact groups
- Manage schedules
- Get check results and uptime
- Get notification information
- Get probe information
You can get the code from our GitHub repository or install it from PyPi via pip. Examples are provided in the README as well as useful information provided by the docstrings that will be shown by your text editor’s completion menus.
Installation
To install nodepingpy
, you can use the pip package manager:
python3 -m pip install nodepingpy
Examples
Below I will provide a few code snippets to demonstrate how you can use the nodepingpy
package to manage your NodePing account via our API.
Get a list of failing checks:
# -*- coding: utf-8 -*-
from pprint import pprint
from nodepingpy import checks
def main():
"""Get failing checks."""
token = "my-secret-token"
failing = checks.get_failing(token)
pprint(failing)
if __name__ == "__main__":
main()
With the output from the API:
{'202306261809OGK26-HM723FI3': {'_id': '202306261809OGK26-HM723FI3',
'created': 1711487757004,
'customer_id': '202306261809OGK26',
'enable': 'active',
'firstdown': 1711487850335,
'interval': 1,
'label': 'Test Check',
'modified': 1711487833733,
'mute': False,
'parameters': {'sens': 2,
'target': 'sholudfail.example.com',
'threshold': 5},
'queue': 'txWlac7EdV',
'runlocations': False,
'state': 0,
'status': 'assigned',
'type': 'PING'}}
The following example will create an HTTP check targeted at ‘https://nodeping.com’:
# -*- coding: utf-8 -*-
from pprint import pprint
from nodepingpy import checks
from nodepingpy.nptypes import checktypes
def main():
"""Create an HTTP check."""
token = "my-secret-token"
args = checktypes.HttpCheck(
"https://example.com", label="Check NodePing", interval=3, runlocations="nam"
)
result = checks.create_check(token, args)
pprint(result)
if __name__ == "__main__":
main()
The check was created and I received this from the API as a response:
{'_id': '202306261809OGK26-FFLHP1UX',
'autodiag': False,
'change': 1711488209387,
'created': 1711488209387,
'customer_id': '202306261809OGK26',
'dep': False,
'enable': 'inactive',
'homeloc': False,
'interval': 3,
'label': 'Check NodePing',
'modified': 1711488209387,
'mute': False,
'parameters': {'follow': False,
'ipv6': False,
'sens': 2,
'target': 'https://example.com/',
'threshold': 5},
'public': False,
'runlocations': ['nam'],
'status': 'modified',
'type': 'HTTP',
'uuid': 'vo5r82j8-twkq-4553-8d0t-02wlfhlgy2x7'}
Lastly, an example of creating a contact on your account:
# -*- coding: utf-8 -*-
from pprint import pprint
from nodepingpy import contacts
from nodepingpy import contacts
def main():
"""Create a NodePing account contact."""
token = "my-secret-token"
name = "Bob Alice"
custrole = "edit"
customerid = "202306261809OGK26"
newaddresses = [
{"address": "me@email.com", "type": "email"},
{"address": "5551238888", "type": "sms"},
]
result = contacts.create(token, customerid, custrole, name, newaddresses)
pprint(result)
if __name__ == "__main__":
main()
And the response I received from the API:
'addresses': {'0CCJ3QUK': {'accountsuppressall': False,
'address': 'me@email.com',
'type': 'email'},
'0CIIIE7K': {'accountsuppressall': False,
'address': '5551238888',
'type': 'sms'}},
'customer_id': '202306261809OGK26',
'custrole': 'edit',
'name': 'Bob Alice',
'sdomain': 'nodeping.com',
'type': 'contact'}
This is only a small part of what the library can do, and the documentation is detailed to get you started on integrating NodePing into your Python project. Give it a try and see how you can improve your uptime monitoring in your Python projects. This code is free and available to download. We encourage pull requests for new features so if you make changes or have requests, feel free to share.
If you aren’t using NodePing yet, you can sign up for a free, 15-day trial and test out monitoring your services today and take advantage of our API in your own Python projects.