· 5 years ago · Nov 14, 2020, 06:46 PM
1import json
2import ovh
3import socket
4import threading
5import time
6
7# Instanciate an OVH Client.
8# You can generate new credentials with full access to your account on
9# the token creation page
10
11client = ovh.Client(
12 endpoint='ovh-eu', # Endpoint of API OVH Europe (List of available endpoints)
13 application_key='XXXXXXXX', # Application Key
14 application_secret='XXXXXX', # Application Secret
15 consumer_key='XXXXXXXXXX', # Consumer Key
16)
17
18def CnameCheck():
19# Get CNAME id
20 global CurrentCNAME
21 id = client.get('/domain/zone/XXXXXXX.fr/record/',
22 fieldType='CNAME', #// Filter the value of fieldType property (like) (type: zone.NamedResolutionFieldTypeEnum)
23 subDomain='monitoring', #// Filter the value of subDomain property (like) (type: string)
24 )
25 id = str(id)[1:-1]
26 result = client.get('/domain/zone/XXXXXX.fr/record/'+ (id))
27 response = json.dumps(result, indent=4)
28 jsoned = json.loads(response)
29 response = jsoned.get("target")
30 CurrentCNAME = response
31
32
33def ServerCheck():
34 global ServerCNAME
35 threading.Timer(5.0, ServerCheck).start()
36 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
37 sock.settimeout(2) #2 Second Timeout
38 result = sock.connect_ex(('XXX.XXX.XXX.XXX',443)) #Ip and machine port
39 if result == 0:
40 ServerCNAME = "up.monitoring"
41 else:
42 ServerCNAME = "down.monitoring"
43
44def CnameUpdate():
45 delete = client.delete('/domain/zone/XXXXX.fr/record/'+ (id))
46 cname = client.post('/domain/zone/albanv.fr/record',
47 fieldType='CNAME', #// Resource record Name (type: zone.NamedResolutionFieldTypeEnum)
48 subDomain='monitoring', #// Resource record subdomain (type: string)
49 target=(ServerCNAME), #// Resource record target (type: string)
50 ttl=60, #// Resource record ttl (type: long)
51