Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:58:23

0001 #!/usr/bin/python
0002 
0003 """
0004 Copied from panda sls_document.py (https://gitlab.cern.ch/ai/it-puppet-hostgroup-vopanda/-/raw/master/code/files/pandaserver/sls_document.py?ref_type=heads)
0005 """
0006 
0007 import json
0008 import requests
0009 import time
0010 
0011 collector_endpoint = 'http://monit-metrics:10012/'
0012 
0013 
0014 class SlsDocument:
0015     def __init__(self):
0016         self.info = {}
0017         self.data = {}
0018         self.id = None
0019         self.producer = 'panda'
0020 
0021     def set_id(self, id_info):
0022         self.id = id_info
0023 
0024     def set_status(self, availability):
0025         if availability in (100, '100'):
0026             self.info['service_status'] = "available"
0027         elif availability in (0, '0'):
0028             self.info['service_status'] = "unavailable"
0029         else:
0030             self.info['service_status'] = "degraded"
0031 
0032     def set_avail_desc(self, avail_desc):
0033         self.info['availabilitydesc'] = avail_desc
0034 
0035     def set_avail_info(self, avail_info):
0036         self.info['availabilityinfo'] = avail_info
0037 
0038     def add_data(self, name, value):
0039         self.data[name] = value
0040 
0041     def get_time(self):
0042         return int(time.time() * 1000)
0043 
0044     def send_document(self, debug=False):
0045         docs = []
0046         if not self.id:
0047             print("No id was set. Will not send")
0048 
0049         if self.info:
0050             self.info['type'] = 'availability'
0051             self.info['serviceid'] = self.id
0052             self.info['producer'] = self.producer
0053             self.info['timestamp'] = self.get_time()
0054             docs.append(self.info)
0055 
0056         if self.data:
0057             self.data['type'] = 'metrics'
0058             self.data['serviceid'] = self.id
0059             self.data['producer'] = self.producer
0060             self.data['timestamp'] = self.get_time()
0061             docs.append(self.data)
0062 
0063         response = requests.post(collector_endpoint, data=json.dumps(docs),
0064                                  headers={"Content-Type": "application/json; charset=UTF-8"})
0065 
0066         if debug:
0067             print('Tried to publish docs {0} with status code: {1}'.format(docs, response.status_code))