Skip to content

Commit

Permalink
[mitre] add metrics (#1423)
Browse files Browse the repository at this point in the history
  • Loading branch information
axelfahy authored Sep 24, 2023
1 parent c676d06 commit 57f142e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion external-import/mitre/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ services:
- CONNECTOR_RUN_AND_TERMINATE=false
- CONNECTOR_LOG_LEVEL=error
- MITRE_INTERVAL=7 # In days
restart: always
restart: always
3 changes: 2 additions & 1 deletion external-import/mitre/src/config.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ connector:
update_existing_data: false
run_and_terminate: false
log_level: 'info'
expose_metrics: False

mitre:
interval: 7 # In days
interval: 7 # In days
9 changes: 8 additions & 1 deletion external-import/mitre/src/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Mitre:
def __init__(self):
config_file_path = os.path.dirname(os.path.abspath(__file__)) + "/config.yml"
config = (
yaml.load(open(config_file_path), Loader=yaml.FullLoader)
yaml.load(open(config_file_path, encoding="utf8"), Loader=yaml.FullLoader)
if os.path.isfile(config_file_path)
else {}
)
Expand Down Expand Up @@ -153,6 +153,7 @@ def retrieve_data(self, url: str) -> Optional[dict]:
urllib.error.ContentTooShortError,
) as urllib_error:
self.helper.log_error(f"Error retrieving url {url}: {urllib_error}")
self.helper.metric.inc("client_error_count")
return None

def add_confidence_to_bundle_objects(self, stix_bundle: dict):
Expand Down Expand Up @@ -189,6 +190,9 @@ def process_data(self):
return

self.helper.log_info(f"Connector will run now {time_now}.")
self.helper.metric.inc("run_count")
self.helper.metric.state("running")

friendly_name = f"MITRE run @ {time_now}"
work_id = self.helper.api.work.initiate_work(
self.helper.connect_id, friendly_name
Expand All @@ -208,6 +212,7 @@ def process_data(self):
update=self.update_existing_data,
work_id=work_id,
)
self.helper.metric.inc("record_send", len(data["objects"]))

message = f"Connector successfully run, storing last_run as {time_now}"
self.helper.log_info(message)
Expand All @@ -227,10 +232,12 @@ def run(self):
self.process_data()
except (KeyboardInterrupt, SystemExit):
self.helper.log_info("Connector stop")
self.helper.metric.state("stopped")
sys.exit(0)
except Exception as e:
self.helper.log_error(str(e))
finally:
self.helper.metric.state("idle")
time.sleep(60)


Expand Down

0 comments on commit 57f142e

Please sign in to comment.