mercredi 25 novembre 2020

OpsGenie : AWS SNS message to Jira Description Wiki markup (+ links to S3 logs and SSM output)

If you're using the AWS SNS opsgenie integration and want to publish to JIRA, you can for example use the following code to present the data in a slightly better way : 


I this use-case I'm using the SNS channel to publish outputs from a system manager (AWS SSM) command that also publishes it's outputs to an S3, so we're using this extraction to provide the direct links to the s3 logs and the SSM run command history.

And in the end, we copy the message we received from the SNS channel "raw"..


In Opsgenie, in the specific Amazon SNS integration (Incoming Amazon SNS), in the Alert Fields, you can for example modify the "Description" so that it transforms the Message received like this :


h3. Details
|| AWS region | {{ TopicArn.extract(/arn:aws:sns:([^:]*):.*/) }}  |
|| Status | {{ Message.extract(/.*"status":"([^"]*)".*/) }} |
|| Instance ID |   {{ Message.extract(/.*"instanceId":"([^"]*)".*/) }}  [(aws link)|https://{{ TopicArn.extract(/arn:aws:sns:([^:]*):.*/) }}.console.aws.amazon.com/ec2/v2/home?region={{ TopicArn.extract(/arn:aws:sns:([^:]*):.*/) }}#InstanceDetails:instanceId={{ Message.extract(/.*"instanceId":"([^"]*)".*/) }}]|
|| Command ID | {{ Message.extract(/.*"commandId":"([^"]*)".*/) }} [(aws cmd)|https://console.aws.amazon.com/systems-manager/run-command/{{ Message.extract(/.*"commandId":"([^"]*)".*/) }}]  [(s3 logs)|https://console.aws.amazon.com/s3/buckets/ssm-output/ssm-log/{{ Message.extract(/.*"commandId":"([^"]*)".*/) }}/{{ Message.extract(/.*"instanceId":"([^"]*)".*/) }}/?region={{ TopicArn.extract(/arn:aws:sns:([^:]*):.*/) }}&showversions=false ]  
|
|| documentName | {{ Message.extract(/.*"documentName":"([^"]*)".*/) }} |
|| requestedDateTime | {{ Message.extract(/.*"requestedDateTime":"([^"]*)".*/) }} |
|| eventTime | {{ Message.extract(/.*"eventTime":"([^"]*)".*/) }} |
h3. Opsgenie info
|| EventType | {{eventType}} |
|| Timestamp (opsgenie) | {{Timestamp}}|
|| Tags | {{tags}} |
|| TopicArn | {{TopicArn}} |
|| Actions | {{actions}} |
h3. Original Message (raw): 
{code}
{{Message}}
{code}



Nb: this might only be available in certain OpsGenie subscriptions unfortunately :-(  

mardi 3 novembre 2020

Gitops & git tracking modifications & Nagios


Context : 

I needed to have all my nagios files on my computer to be able to run some python scripts trying to figure out what refactoring needed to be done, and identifying gaps in the configuration I inherited.

I took that opportunity to version all our nagios configuration files, with a git repository configured at the /usr/local/nagios/etc level.


That proved itself useful to gain some confidence that we're not going to loose anything.


Initial idea was taken from :  


Script to automatically commit changes done in Nagios and push them to the central repo. 
NB : still a few things to investigate, but ...

auto-git-commit-push.sh

#!/bin/bash
cd /usr/local/nagios/etc \
&& /bin/git pull \
&& /bin/git add -A \
&& /bin/git commit -m "updated nagios dynamic files $(date) -- automatic commit" \
&& /bin/git push origin master \
&&  /bin/git pull \
&& if ! $(grep -lr '<<<<<<<' . ) ; then grep -lr '<<<<<<<' . | xargs git checkout --ours; ./$0; fi


identified issues :
* why so many conflicts while we are only modifying and thus commiting files locally and pushing them !?
* I thought about putting in place a workflow that would take the files from the git repository and push them to nagios import with some git hooks, but I eventually found the Nagios REST API which is way easier to delivery to the team handling those creations : just 1 script per use-case and they'll do everything at once.