From fead4a0eb8a7d7fa5f3054729e1af412f6baaf74 Mon Sep 17 00:00:00 2001 From: John Schoewe Date: Fri, 14 Aug 2020 12:41:48 -0400 Subject: [PATCH 1/4] Added sensor for checking if the orion API is down --- CHANGES.md | 5 ++ pack.yaml | 2 +- sensors/solarwinds_api_check.py | 78 +++++++++++++++++++++++++++++++ sensors/solarwinds_api_check.yaml | 9 ++++ triggers/solarwinds_restart.yaml | 4 ++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 sensors/solarwinds_api_check.py create mode 100644 sensors/solarwinds_api_check.yaml create mode 100644 triggers/solarwinds_restart.yaml diff --git a/CHANGES.md b/CHANGES.md index e76a01a..4eda8de 100755 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,10 @@ # Changelog +## 0.7.12 + +- Added sensor to check if the orion API is down and fires a trigger to restart the service if it is + Contributed by John Schoewe (Encore Technologies) + ## 0.7.11 - Added action to enable and disable maintenance mode on a given node diff --git a/pack.yaml b/pack.yaml index 3cccb5f..c3a4218 100755 --- a/pack.yaml +++ b/pack.yaml @@ -8,7 +8,7 @@ keywords: - ncm - npm - monitoring -version: 0.7.11 +version: 0.7.12 python_versions: - "2" - "3" diff --git a/sensors/solarwinds_api_check.py b/sensors/solarwinds_api_check.py new file mode 100644 index 0000000..eadf562 --- /dev/null +++ b/sensors/solarwinds_api_check.py @@ -0,0 +1,78 @@ +from orionsdk import SwisClient +from requests.exceptions import ConnectionError +from st2reactor.sensor.base import PollingSensor + + +class SolarwindsApiCheck(PollingSensor): + """ + * self.sensor_service + - provides utilities like + - get_logger() - returns logger instance specific to this sensor. + - dispatch() for dispatching triggers into the system. + * self._config + - contains parsed configuration that was specified as + config.yaml in the pack. + """ + + def setup(self): + # Setup stuff goes here. For example, you might establish connections + # to external system once and reuse it. This is called only once by the system. + server = self._config['orion_host'] + user = self._config['orion_user'] + passwd = self._config['orion_password'] + + self._logger = self.sensor_service.get_logger(name=self.__class__.__name__) + + self.client = SwisClient(server, user, passwd) + self._logger.debug(self.client) + + pass + + def poll(self): + # This is where the crux of the sensor work goes. + # This is called every self._poll_interval. + # For example, let's assume you want to query ec2 and get + # health information about your instances: + # some_data = aws_client.get('') + # payload = self._to_payload(some_data) + # # _to_triggers is something you'd write to convert the data format you have + # # into a standard python dictionary. This should follow the payload schema + # # registered for the trigger. + # self.sensor_service.dispatch(trigger, payload) + # # You can refer to the trigger as dict + # # { "name": ${trigger_name}, "pack": ${trigger_pack} } + # # or just simply by reference as string. + # # i.e. dispatch(${trigger_pack}.${trigger_name}, payload) + # # E.g.: dispatch('examples.foo_sensor', {'k1': 'stuff', 'k2': 'foo'}) + # # trace_tag is a tag you would like to associate with the dispatched TriggerInstance + # # Typically the trace_tag is unique and a reference to an external event. + try: + self._logger.debug('SolarWinds API check poll') + + # We need to run a query because the connection itself will not return an error + swql = """SELECT Name, MethodName, EntityName + FROM Metadata.Verb where CanInvoke=@CanInvoke""" + kargs = {'CanInvoke': True} + orion_data = self.client.query(swql, **kargs) + self._logger.debug("SolarWinds API query successful") + self._logger.debug(orion_data) + except ConnectionError: + self._logger.debug('Dispatch SolarWinds services restart trigger') + self.sensor_service.dispatch(trigger='orion.solarwinds_restart') + + def cleanup(self): + # This is called when the st2 system goes down. You can perform cleanup operations like + # closing the connections to external system here. + pass + + def add_trigger(self, trigger): + # This method is called when trigger is created + pass + + def update_trigger(self, trigger): + # This method is called when trigger is updated + pass + + def remove_trigger(self, trigger): + # This method is called when trigger is deleted + pass diff --git a/sensors/solarwinds_api_check.yaml b/sensors/solarwinds_api_check.yaml new file mode 100644 index 0000000..42383c7 --- /dev/null +++ b/sensors/solarwinds_api_check.yaml @@ -0,0 +1,9 @@ +--- + class_name: "SolarwindsApiCheck" + entry_point: "solarwinds_api_check.py" + description: "Verify that the SolarWinds API is up and accepting connections." + poll_interval: 30 + trigger_types: + - + name: "solarwinds_restart" + description: "An example trigger." diff --git a/triggers/solarwinds_restart.yaml b/triggers/solarwinds_restart.yaml new file mode 100644 index 0000000..3d5facb --- /dev/null +++ b/triggers/solarwinds_restart.yaml @@ -0,0 +1,4 @@ +name: solarwinds_restart +description: Trigger to restart the SolarWinds services +payload_schema: + type: object From 31a9048fcc110d2ed2c178ab02f03245d7ab74e0 Mon Sep 17 00:00:00 2001 From: John Schoewe Date: Fri, 14 Aug 2020 13:07:27 -0400 Subject: [PATCH 2/4] Renamed trigger to solarwinds_api_down --- CHANGES.md | 2 +- pack.yaml | 2 +- sensors/solarwinds_api_check.py | 36 +++---------------------------- triggers/solarwinds_api_down.yaml | 4 ++++ triggers/solarwinds_restart.yaml | 4 ---- 5 files changed, 9 insertions(+), 39 deletions(-) create mode 100644 triggers/solarwinds_api_down.yaml delete mode 100644 triggers/solarwinds_restart.yaml diff --git a/CHANGES.md b/CHANGES.md index 4eda8de..15781f1 100755 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,6 @@ # Changelog -## 0.7.12 +## 0.8.0 - Added sensor to check if the orion API is down and fires a trigger to restart the service if it is Contributed by John Schoewe (Encore Technologies) diff --git a/pack.yaml b/pack.yaml index c3a4218..12ded05 100755 --- a/pack.yaml +++ b/pack.yaml @@ -8,7 +8,7 @@ keywords: - ncm - npm - monitoring -version: 0.7.12 +version: 0.8.0 python_versions: - "2" - "3" diff --git a/sensors/solarwinds_api_check.py b/sensors/solarwinds_api_check.py index eadf562..4b75748 100644 --- a/sensors/solarwinds_api_check.py +++ b/sensors/solarwinds_api_check.py @@ -5,18 +5,10 @@ class SolarwindsApiCheck(PollingSensor): """ - * self.sensor_service - - provides utilities like - - get_logger() - returns logger instance specific to this sensor. - - dispatch() for dispatching triggers into the system. - * self._config - - contains parsed configuration that was specified as - config.yaml in the pack. + This sensor is used to fire a trigger when it detects that the API is down """ def setup(self): - # Setup stuff goes here. For example, you might establish connections - # to external system once and reuse it. This is called only once by the system. server = self._config['orion_host'] user = self._config['orion_user'] passwd = self._config['orion_password'] @@ -29,23 +21,6 @@ def setup(self): pass def poll(self): - # This is where the crux of the sensor work goes. - # This is called every self._poll_interval. - # For example, let's assume you want to query ec2 and get - # health information about your instances: - # some_data = aws_client.get('') - # payload = self._to_payload(some_data) - # # _to_triggers is something you'd write to convert the data format you have - # # into a standard python dictionary. This should follow the payload schema - # # registered for the trigger. - # self.sensor_service.dispatch(trigger, payload) - # # You can refer to the trigger as dict - # # { "name": ${trigger_name}, "pack": ${trigger_pack} } - # # or just simply by reference as string. - # # i.e. dispatch(${trigger_pack}.${trigger_name}, payload) - # # E.g.: dispatch('examples.foo_sensor', {'k1': 'stuff', 'k2': 'foo'}) - # # trace_tag is a tag you would like to associate with the dispatched TriggerInstance - # # Typically the trace_tag is unique and a reference to an external event. try: self._logger.debug('SolarWinds API check poll') @@ -57,22 +32,17 @@ def poll(self): self._logger.debug("SolarWinds API query successful") self._logger.debug(orion_data) except ConnectionError: - self._logger.debug('Dispatch SolarWinds services restart trigger') - self.sensor_service.dispatch(trigger='orion.solarwinds_restart') + self._logger.debug('Dispatch SolarWinds API down trigger') + self.sensor_service.dispatch(trigger='orion.solarwinds_api_down') def cleanup(self): - # This is called when the st2 system goes down. You can perform cleanup operations like - # closing the connections to external system here. pass def add_trigger(self, trigger): - # This method is called when trigger is created pass def update_trigger(self, trigger): - # This method is called when trigger is updated pass def remove_trigger(self, trigger): - # This method is called when trigger is deleted pass diff --git a/triggers/solarwinds_api_down.yaml b/triggers/solarwinds_api_down.yaml new file mode 100644 index 0000000..1f91b3b --- /dev/null +++ b/triggers/solarwinds_api_down.yaml @@ -0,0 +1,4 @@ +name: solarwinds_api_down +description: Trigger that gets fired when the orion API is unreachable +payload_schema: + type: object diff --git a/triggers/solarwinds_restart.yaml b/triggers/solarwinds_restart.yaml deleted file mode 100644 index 3d5facb..0000000 --- a/triggers/solarwinds_restart.yaml +++ /dev/null @@ -1,4 +0,0 @@ -name: solarwinds_restart -description: Trigger to restart the SolarWinds services -payload_schema: - type: object From 4c7d7fde3f880b6b905fbe28a726f285b303da29 Mon Sep 17 00:00:00 2001 From: John Schoewe Date: Mon, 24 Aug 2020 12:48:42 -0400 Subject: [PATCH 3/4] Updated trigger name and increased polling interval --- sensors/solarwinds_api_check.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sensors/solarwinds_api_check.yaml b/sensors/solarwinds_api_check.yaml index 42383c7..965b293 100644 --- a/sensors/solarwinds_api_check.yaml +++ b/sensors/solarwinds_api_check.yaml @@ -2,8 +2,8 @@ class_name: "SolarwindsApiCheck" entry_point: "solarwinds_api_check.py" description: "Verify that the SolarWinds API is up and accepting connections." - poll_interval: 30 + poll_interval: 60 trigger_types: - - name: "solarwinds_restart" - description: "An example trigger." + name: "solarwinds_api_down" + description: "Trigger to fire when the solarwinds API is down" From 7924bc5fa45613370bfc6a6ec1415ccec778841a Mon Sep 17 00:00:00 2001 From: John Schoewe Date: Thu, 8 Oct 2020 09:52:21 -0400 Subject: [PATCH 4/4] Updated polling interval for sensor to every 15 minutes instead of every minute --- sensors/solarwinds_api_check.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sensors/solarwinds_api_check.yaml b/sensors/solarwinds_api_check.yaml index 965b293..a794288 100644 --- a/sensors/solarwinds_api_check.yaml +++ b/sensors/solarwinds_api_check.yaml @@ -1,8 +1,8 @@ --- class_name: "SolarwindsApiCheck" entry_point: "solarwinds_api_check.py" - description: "Verify that the SolarWinds API is up and accepting connections." - poll_interval: 60 + description: "Verify that the SolarWinds API is up and accepting connections. Poll every 15 minutes." + poll_interval: 900 trigger_types: - name: "solarwinds_api_down"