-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheck_handle_api.py
executable file
·110 lines (86 loc) · 3.52 KB
/
check_handle_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
import argparse
import sys
import b2handle
import traceback
from b2handle.clientcredentials import PIDClientCredentials
from b2handle.handleclient import EUDATHandleClient
from b2handle.handleexceptions import *
from requests.exceptions import SSLError
import signal
from time import strftime,gmtime
TEST_SUFFIX='NAGIOS-' + strftime("%Y%m%d-%H%M%S",gmtime())
VALUE_ORIG='http://www.' + TEST_SUFFIX + '.com/1'
VALUE_AFTER='http://www.' + TEST_SUFFIX + '.com/2'
def handler(signum, stack):
print "UNKNOWN: Timeout reached, exiting."
sys.exit(3)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='EPIC API create, read, update, delete probe')
req = parser.add_argument_group('required arguments')
req.add_argument('-f', '--file', action='store', dest='json_file', required=True,
help='JSON credentials file')
req.add_argument('-p', '--prefix', action='store', dest='prefix',
help='prefix to test')
req.add_argument('-t', '--timeout', action='store', dest='timeout',
help='timeout')
parser.add_argument('-d', '--debug', action='store_true', dest='debug',
help='debug mode')
param = parser.parse_args()
if param.timeout and int(param.timeout) > 0 :
signal.signal(signal.SIGALRM, handler)
signal.alarm(int(param.timeout))
try:
print "Creating credentials"
cred = PIDClientCredentials.load_from_JSON(param.json_file)
client = EUDATHandleClient.instantiate_with_credentials(cred)
print('PID prefix ' + cred.get_prefix())
print('Server ' + cred.get_server_URL())
handle = cred.get_prefix() + '/' + TEST_SUFFIX
# Create test
print "Creating handle " + handle
create_result = client.register_handle(
handle, VALUE_ORIG)
if create_result == handle:
print "OK: Create handle successful."
else:
print "CRITICAL: Create handle returned unexpected response."
sys.exit(2)
# Read test
key = 'URL'
read_value = client.get_value_from_handle(
handle, key)
if read_value == VALUE_ORIG:
print "OK: Read handle successful."
else:
print "CRITICAL: Read handle returned unexpected response."
client.delete_handle(handle)
sys.exit(2)
# Modify test
client.modify_handle_value(
handle, **{key: VALUE_AFTER} )
get_value_result = client.get_value_from_handle(
handle, key)
if get_value_result == VALUE_AFTER:
print "OK: Modify handle successful."
else:
print "CRITICAL: Modify handle value returned unexpected value."
print "Expected : " + VALUE_AFTER
print "Returned : " + get_value_result
client.delete_handle(handle)
sys.exit(2)
# Delete test
delete_result = client.delete_handle(handle)
print "OK: Delete handle successful."
except (GenericHandleError, CredentialsFormatError, HandleSyntaxError, HandleNotFoundException, HandleAuthenticationError, SSLError) as e:
if param.debug :
print "CRITICAL: " + traceback.format_exc()
else :
print "CRITICAL: " + str(e)
sys.exit(2)
except Exception as e:
if param.debug :
print "UNKNOWN: " + traceback.format_exc()
else:
print "UNKNOWN: " + str(e)
sys.exit(3)