-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_veeam_license
173 lines (149 loc) · 5.31 KB
/
check_veeam_license
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python
#####################################################
#
# Veeam Backup servers licence expiration check
#
# Last modified : 08.10.2021
# Author : Allan Resin
# Version : 1.5.1
#
#####################################################
#Import all needed modules
from __future__ import print_function
import sys, getopt
import argparse
import requests
import time
import json
from datetime import datetime
#####################################################
#
# Set the URL of the Veeam SPC server + port of the API endpoint
varURL = "https://URL_VEEAM:1281"
#
# Set the username and password for the user that have an access to the API
varUser = "API_USER"
varPasswd = "API_USER_PASS"
#
# Change the threshold for alerts on Centreon.
varOk = 15 #Before how many days it will be OK
varWarning = 15 #After how many days it will be WARNING
varCritical = 0 #At how many days it will be CRITICAL
#
#####################################################
#Add the --host argument
parser=argparse.ArgumentParser()
parser.add_argument('--host', help='Specify the veeam server that you want to check for the licence. ex')
parser.add_argument('--force', nargs='?', default=None, help='If you want to force an hostname, it will override the --host arg. Not mandatory')
args=parser.parse_args()
#Check to quit the script in case of the arg isn't specified
if (args.host is None):
print("usage: check_veeam_licence.py [-h] [--host HOST]")
print("error: argument --host: expected one argument")
sys.exit(2)
#Set some variables for after
licenceID = None
#Check if the --force argument is provided. If yes, it's used to force another hostname.
if args.force is not None:
hostArg = args.force.upper()
pass
else :
hostArg = args.host.upper()
#Create a payload that will ask the API to login and get back the Auth Key
def apiGet_token():
headers = {
"Connection": "close",
"Authorization": "Bearer",
"Content-Type": "application/json",
}
data = "grant_type=password&username="+varUser+"&password="+varPasswd
url = varURL+'/token'
response = requests.post(url, data=data, headers=headers)
return response
#Create a payload that will ask the API to get all Veeam Backup servers licence infos
def apiGet_licenceID(token):
headers = {
"Connection": "close",
"Authorization": "Bearer "+token,
}
url = varURL+"/v2/licensing/backupserverLicenses"
response2 = requests.get(url, headers=headers)
return response2
#Create a payload that will ask the API to get all Veeam Cloud Connect licence infos
def apiGet_licenceCC(token):
headers = {
"Connection": "close",
"Authorization": "Bearer "+token,
}
url = varURL+"/v2/licensing/cloudconnectLicenses"
response3 = requests.get(url, headers=headers)
return response3
#Try/Except for getting API response.
try:
r = apiGet_token()
except requests.exceptions.RequestException as e:
print("API CALL FAILED ... : ")
print(e)
raise "API CALL FAILED..."
sys.exit(2)
#Load in JSON the response of the API
data_token = json.loads(r.content)
#Try/Except for getting API response. Sent as arg to function --> token
try:
r2 = apiGet_licenceID(data_token["access_token"])
except requests.exceptions.RequestException as e:
print("API CALL FAILED ... : ")
print(e)
raise "API CALL FAILED..."
sys.exit(2)
#Try/Except for getting API response. Sent as arg to function --> token
try:
r3 = apiGet_licenceCC(data_token["access_token"])
except requests.exceptions.RequestException as e:
print("API CALL FAILED ... : ")
print(e)
raise "API CALL FAILED..."
sys.exit(2)
#Get licence data in JSON
data_licences = json.loads(r2.content)
#Get CLoud Connect licence data in JSON
data_licencesCC = json.loads(r3.content)
#Itterate over the JSON data to take the licence expiration date when "backupServerName" is the same as the --host provided
for x in data_licences:
if (x['backupServerName'] == hostArg):
licenceID = x['id']
licenceEXP = x['licenseExpirationDate']
pass
#If no licence found, stop the script
if (licenceID is None):
for x in data_licencesCC:
if (x['backupServerName'] == hostArg):
licenceID = x['id']
licenceEXP = x['licenseExpirationDate']
pass
else:
print("Licence not found for the given Veeam server. Exiting ...")
sys.exit(1)
#Function that do maths to calculate how many days left before expiration
def expiryMaths(expiry):
tTmp = datetime.today().strftime('%Y-%m-%d')
tExp = datetime.date(datetime.strptime(expiry, "%Y-%m-%dT%H:%M:%SZ"))
tNow = datetime.date(datetime.strptime(tTmp, "%Y-%m-%d"))
daysBeforeExp = tExp - tNow
return daysBeforeExp.days
#Print the final output (Format --> Days before expiration)
daysBeforeExpI = int(expiryMaths(licenceEXP))
#Uncomment this only for debug, to simulate a number of day for the licence expiration
#daysBeforeExpI = 0
if daysBeforeExpI > varOk:
print("OK : Licence expires in : "+str(daysBeforeExpI)+" days.")
sys.exit(0)
pass
if daysBeforeExpI < varWarning:
if daysBeforeExpI <= varCritical:
print("CRITICAL : Licence expires in : "+str(daysBeforeExpI)+" days.")
sys.exit(2)
pass
print("WARNING : Licence expires in : "+str(daysBeforeExpI)+" days.")
sys.exit(1)
pass