import snippets from gitlab
This commit is contained in:
62
check_frappe_doctype/check-frappe-objects.py
Normal file
62
check_frappe_doctype/check-frappe-objects.py
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import requests
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(description='Check Frappe doctype for recent updates.')
|
||||
parser.add_argument('--doctype', required=True, help='The doctype to check.')
|
||||
parser.add_argument('--warning', type=int, required=True, help='Warning threshold in minutes.')
|
||||
parser.add_argument('--critical', type=int, required=True, help='Critical threshold in minutes.')
|
||||
parser.add_argument('--url', required=True, help='Frappe site URL.')
|
||||
parser.add_argument('--api_key', required=True, help='API key for authentication.')
|
||||
parser.add_argument('--api_secret', required=True, help='API secret for authentication.')
|
||||
return parser.parse_args()
|
||||
|
||||
def get_recent_objects(url, api_key, api_secret, doctype):
|
||||
headers = {
|
||||
'Authorization': f'token {api_key}:{api_secret}',
|
||||
}
|
||||
params = {
|
||||
'fields': '["name", "modified"]',
|
||||
'limit_page_length': 1,
|
||||
'order_by': 'modified desc'
|
||||
}
|
||||
response = requests.get(f'{url}/api/resource/{doctype}', headers=headers, params=params)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
return data['data']
|
||||
else:
|
||||
print(f"Failed to fetch data: {response.status_code}")
|
||||
sys.exit(3)
|
||||
|
||||
def check_for_alerts(recent_date, warning_limit, critical_limit):
|
||||
now = datetime.now()
|
||||
time_diff = now - recent_date
|
||||
|
||||
if time_diff > timedelta(minutes=critical_limit):
|
||||
print(f"CRITICAL: Last update was {time_diff} ago")
|
||||
sys.exit(2)
|
||||
elif time_diff > timedelta(minutes=warning_limit):
|
||||
print(f"WARNING: Last update was {time_diff} ago")
|
||||
sys.exit(1)
|
||||
else:
|
||||
print("OK: Update within acceptable range")
|
||||
sys.exit(0)
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
|
||||
recent_objects = get_recent_objects(args.url, args.api_key, args.api_secret, args.doctype)
|
||||
if recent_objects:
|
||||
# example output: {'name': 'adac-fifo', 'modified': '2023-09-06 15:02:37.254758'}
|
||||
last_modified = datetime.strptime(recent_objects[0]['modified'], "%Y-%m-%d %H:%M:%S.%f")
|
||||
check_for_alerts(last_modified, args.warning, args.critical)
|
||||
else:
|
||||
print("No objects found. Exiting with OK status.")
|
||||
sys.exit(0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
31
check_frappe_doctype/readme.md
Normal file
31
check_frappe_doctype/readme.md
Normal file
@@ -0,0 +1,31 @@
|
||||
## Check Frappe doctype objects
|
||||
|
||||
This script checks the last modified date of a Frappe doctype and alerts if it's older than a certain threshold.
|
||||
It uses the Frappe API to fetch the last modified date of the doctype and compares it to the current time.
|
||||
|
||||
```
|
||||
$ python3 check-frappe-objects.py -h
|
||||
usage: check-frappe-objects.py [-h] --doctype DOCTYPE --warning WARNING --critical CRITICAL --url URL --api_key API_KEY --api_secret
|
||||
API_SECRET
|
||||
|
||||
Check Frappe doctype for recent updates.
|
||||
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
--doctype DOCTYPE The doctype to check.
|
||||
--warning WARNING Warning threshold in minutes.
|
||||
--critical CRITICAL Critical threshold in minutes.
|
||||
--url URL Frappe site URL.
|
||||
--api_key API_KEY API key for authentication.
|
||||
--api_secret API_SECRET
|
||||
API secret for authentication.
|
||||
```
|
||||
|
||||
#### Usage example
|
||||
|
||||
```
|
||||
$ ./check-frappe-objects.py --url https://app.domain.com --doctype "Doctype Name" --warning 10 --critical 20 --api_key XXXXXXXXXXX --api_secret XXXXXXXXXXX
|
||||
CRITICAL: Last update was 190 days, 19:05:17.619701 ago
|
||||
$ echo $?
|
||||
2
|
||||
```
|
||||
Reference in New Issue
Block a user