alerta / alerta-contrib

Contributed integrations, plugins and custom webhooks
http://alerta.io
MIT License
119 stars 171 forks source link

I add new Otions to URLMON #63

Closed ST2Labs closed 7 years ago

ST2Labs commented 8 years ago

Here My Code:

I add options to add new Resource without to Stop urlmon.py

web.json

{
        "1":{
            "resource": "www.svtcloud.com",
            "url": "https://www.svtcloud.com",
            "environment": "urlmon",
            "customer": "SVT",
            "service": ["SVTCloud", "Monitoring"],
            "tags": ["SVTCloud", "WEB"]
        },

    "2":{
        "resource": "hacking-etico.com",
        "url": "http://hacking-etico.com",
        "environment": "urlmon",
        "customer": "SVT",
        "service": ["Hacking-Etico", "Monitoring"],
        "tags": ["Blog", "SVTCloud", "WEB"]
    },

    "3":{
        "resource": "tacol.svtcloud.com",
        "url": "https://tacol.svtcloud.com",
        "environment": "urlmon",
        "customer": "SVT",
        "service": ["MSOC Intranet", "Monitoring"],
        "tags": ["Confluence", "Intranet", "SVTCloud", "MSOC", "WEB"]
    },

    "4":{
        "resource": "www.semic.es",
        "url": "http://www.semic.es",
        "environment": "urlmon",
        "customer": "SVT",
        "service": ["SEMIC WEB", "Monitoring"],
        "tags": ["SEMIC", "MSOC", "WEB"]
    },
"5":{
    "resource": "www.kateosllll.es",
        "url": "http://www.s3kateoslll.es",
        "environment": "urlmon",
        "customer": "SVT",
        "service": ["SEMIC WEB", "Monitoring"],
        "tags": ["SEMIC", "MSOC", "WEB"]
}

}

Now I made change into URLMON.py Change only :

class UrlmonDaemon(object):

def init(self):

    self.shuttingdown = False
    self.webs = self.loadSetting("web.json")

def loadSetting(self, filepath):
    import json
    try:
        l = []
        with open(filepath, 'rb') as f:
            d = json.loads(f.read(), 'utf-8')
            for k in d:
                l.append(d[k])

        f.close()
        return l
    except TypeError as e:
        print  (e)
        sys.exit(2)
    except ValueError as e:
        print (e)
        sys.exit(2)

def run(self):

    self.running = True
    self._cached_stamp = 0
    self.filename = "web.json"
    self.queue = Queue.Queue()
    self.api = self.api = ApiClient(endpoint=settings.ENDPOINT,
                                    key=settings.API_KEY)

    # Start worker threads
    LOG.debug('Starting %s worker threads...', SERVER_THREADS)
    for i in range(SERVER_THREADS):
        w = WorkerThread(self.queue, self.api)
        try:
            w.start()
        except Exception, e:
            LOG.error('Worker thread #%s did not start: %s', i, e)
            continue
        LOG.info('Started worker thread: %s', w.getName())

    while not self.shuttingdown:
        try:
            # Check change into web settings json file
            stamp = os.stat(self.filename).st_mtime
            if stamp != self._cached_stamp:
                self._cached_stamp = stamp
                c = self.loadSetting(self.filename)
                self.webs = c
            else:
                c = self.webs
            #for check in settings.checks:
            for check in c:
                self.queue.put((check, time.time()))

            LOG.debug('Send heartbeat...')
            heartbeat = Heartbeat(tags=[__version__])
            try:
                self.api.send(heartbeat)
            except Exception, e:
                LOG.warning('Failed to send heartbeat: %s', e)

            time.sleep(LOOP_EVERY)
            LOG.info('URL check queue length is %d', self.queue.qsize())

        except (KeyboardInterrupt, SystemExit):
            self.shuttingdown = True

    LOG.info('Shutdown request received...')
    self.running = False

    for i in range(SERVER_THREADS):
        self.queue.put(None)
    w.join()
ST2Labs commented 8 years ago

Will be interesting add options to control the loop cheking time for each resource individially !

This value could add into web.json settings.

Thanks in advance

satterly commented 7 years ago

Thanks. I'll consider this enhancement the next time I need to make changes to the code.