Execute shell script through prometheus alert manger - linux

Please let me know if I can execute a shell script on the same server as Prometheus/alertmanager on an alert trigger?
If so, help me with the configurations.

You can use Prometheus-am-executor to run any shell or even python script too.
Here is sample.yml file
listen_address: ":8094" # Where alertmanager sending alerts
# Display more output
verbose: true
commands:
- cmd: python3
args: ["script.py"] # Script which you want to execute

A maintained alternative is https://github.com/adnanh/webhook which allows you to do install local webhooks with scripts attached.
Example config:
- id: redeploy-webhook
execute-command: "/var/scripts/redeploy.sh"
command-working-directory: "/var/webhook"
Default port of the webhook process is 9000, so the following URL would execute the redeploy.sh script from above config example.
http://yourserver:9000/hooks/redeploy-webhook
Which can be then used in your alertmanager config:
receivers:
- name: 'general'
webhook_config:
- url: http://yourserver:9000/hooks/redeploy-webhook

Related

Windows powershell messagebox using Ansible

I tried to create a task in ansible playbook using win_powershell module but everytime I run the playbook, the task is hanging. Does anyone have a solution ?
I`m pretty new to ansible.
Here is the yml file:
---
- name: notification
hosts: windows
become: false
gather_facts: false
tasks:
- name: message
ansible.windows.win_powershell:
script: |
Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show('message')
Perhaps one of the following modules might be more suitable than running a powershell command. You'd need to install the community modules for either of these.
The community.windows.win_msg module.
Usage:
- name: Display message.
community.windows.win_msg:
msg: 'message'
Ansible also supports toast notifications via community.windows.win_toast module

How to run Cypress tests on circle-ci orb using Cypress-tags

I am trying to run a small test collection with Cypress using the cucumber plugin and the official Circle-ci Orb.
I've been going through the documentation and I've got them running without issues locally. The script I'm using to run them locally is this one:
"test:usa": "cypress-tags run --headless -b chrome -e TAGS='#usa'"
*Note the cypress-tags command and the TAGS option.
For the CI I use the official Circle-ci Orb and have a configuration like this:
- cypress/run:
name: e2e tests
executor: with-chrome
record: true
parallel: true
parallelism: 2
tags: 'on-pr'
group: 2x-chrome
ci-build-id: '${CIRCLE_BUILD_NUM}'
requires:
- org/deployment
As you can read, I want to raise 2 machines where I divide my feature files, setting the tag to 'on-pr' and grouping the run under '2x-chrome' using as well the ci-build-id.
The thing is that the official Orb uses the cypress run command which does not filter scenarios by their tags, so it is of no use here. My options were:
Using the command parameter in the orb to call the required script as I do locally:
command: npm run test:usa
My problem with this option is that the parallel configuration does not work as expected so I discarded it.
I tried to pass the TAGS parameter as an env var within the Circle-ci executor to see if the Orb was able to see it, but it was of no use as the Orb does not use cypress-tags run but cypress run.
environment:
TAGS: '#usa'
At this point my question is, is there a workaround to this (using cypress-tags in the Circle-ci orb) or shall I opt for a different way of testing this?
Thanks in advance

Python CRON google flexible app engine not working

I have the following cron.yaml:
cron:
- description: "TEST_TEST_TEST"
- url: /cronBatchClean
- schedule: every 2 minutes
And then in app.yaml:
service: environ-flexible
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app
runtime_config:
python_version: 3
With this as main.py:
from flask import Flask, request
import sys
app = Flask(__name__)
#app.route('/cronBatchClean')
def cronBatchClean():
print("CRON_CHECK", file=sys.stderr)
return "CRON_CHECK"
When I type in the full URL, I receive "CRON_CHECK" on screen but this doesn't seem to be executing. Also in App Engine dashboard, when I click on CRON jobs there aren't any listed.
Any help in getting this to execute would be much appreciated,
Thanks :)
EDIT 1
I now have the cron task executing but I'm receiving a 404 error. When I type the full URL (that is - https://.appspot.com/cronBatchClean) the respective code executes.
I added a GET handler but I'm still not receiving any luck.
#app.route('/cronBatchClean', methods=['GET'])
def cronBatchClean():
print("CRON_JOB_PRINT", file=sys.stderr)
return "CRON_CHECK"
In the cron.yaml there are unnecessary “-” characters, that are starting the new list. YAML Syntax
Correct format for Cron Jobs cron.yaml, see Google Cloud documentation:
cron:
- description: "TEST_TEST_TEST"
url: /cronBatchClean
schedule: every 2 minutes
To deploy Cron Job use gcloud command :
$ gcloud app deploy cron.yaml
To solve this problem i changed the service name to default then i deploy with the default service the cron job task path was pointed to the default path of app engine so that when the task was scheduled 404 error raised because the path doesn't match when the service name set to "environ-flexible"
In app.yaml
change :
service: environ-flexible
to
service: default

npm start fails to run with AWS Code Deploy on AWS Windows instances

​I am trying to deploy a Node.js application on windows EC2 instances. Deployment finishes successfully but node server is not started automatically on those instances. I've to login to each instance to run command node app.js to start node server. I tried to include this command in appspec.yml file but then I got below error,
LifecycleEvent - ApplicationStart
Script - node_start.bat
[stdout]
[stdout]C:\Windows\system32>cd C:/host/
[stdout]
[stdout]C:\host>npm start
[stderr]'npm' is not recognized as an internal or external command,
[stderr]operable program or batch file.
​
My appspec.yml file is as below,
version: 0.0
os: windows
files:
- source: app.js
destination: c:\host
- source: package.json
destination: c:\host
- source: \node_modules
destination: c:\host\node_modules
- source: node_start.bat
destination: c:\host
- source: before_install.bat
destination: c:\host
hooks:
AfterInstall:
- location: before_install.bat
timeout: 300
ApplicationStart:
- location: node_start.bat
timeout: 300
Node is already installed on those two instances and Path variable is also properly set. Logging manually to those servers and running command npm start works perfectly fine. It fails only though AWS Code deploy.
I want to introduce AWS Lambda function also after this step to do health check.
Any help to resolve this issue would be greatly appreciated.
The issue isn't really related to CodeDeploy. CodeDeploy simply runs the script that you give it; in your case, node_start.bat. Perhaps, this question answers the issue that you're really having. CodeDeploy has no knowledge of your application unless you tell it.
You will likely either need to edit node_start.bat or your environment so that npm is a valid command.
Here are a couple of suggestions to help your case:
Test your appspec and scripts
You can test your appspec.yml and related scripts using the CodeDeploy local CLI, which comes with the agent.
Validate that your service is running
Obviously, it's not awesome if your deployment succeeds, but your application is not actually running. However, you can use the ValidateService lifecycle hook to confirm that your application is actually running or any other validation. You can include a script that can see that the process is running, confirm that no errors are getting logged, run tests, or whatever else you might want.

how to handle never ending loop linux command - ansible

Please refer this link to understand what I have done.
short description
I need to run top command in remote machine and get that result content then save that file in local machine
test.yml
---
- hosts: webservers
remote_user: root
tasks:
- name: 'Copy top.sh to remote machine'
synchronize: mode=push src=top.sh dest=/home/raj
- name: Execute the script
command: sh /home/raj/top.sh
async: 45
poll: 5
- name: 'Copy system.txt to local machine'
synchronize: mode=pull src=system.txt dest=/home/bu
top.sh
#!/bin/bash
top > system.txt
problem
top.sh never end so I am trying to every five seconds poll the result and copy into local machine but it is not working.it throws below error.
stderr: top: failed tty get
<job 351267881857.24744> FAILED on 192.168.1.7
note: I got this error only when I include async and poll option
Hello Bilal I Hope this is useful for you
your syntax: using poll:5 follw this link http://docs.ansible.com/ansible/playbooks_async.html
poll is using wait on the task to complete. But top command doesn,t stop until use stop or system shutdown. use poll:0
" Alternatively, if you do not need to wait on the task to complete, you may “fire and forget” by specifying a poll value of 0:"
Now forget the task, collect top result file from remote and store to local use below syntax
- hosts: webservers
remote_user: root
tasks:
- name: 'Copy top.sh to remote machine'
synchronize: mode=push src=top.sh dest=/home/raj
- name: collecting top result
command: sh /home/raj/top.sh
async: 45
poll: 0
- name: 'Copy top command result to local machine'
synchronize: mode=pull src=/home/raj/Top.txt dest=/home/raj2/Documents/Ansible
top.sh:
#!/bin/bash
top -b > /home/raj/Top.txt
This is works for me. ping me if you have any problem.
Do you need to run the top command itself, or is this just an example of a long-running program you want to monitor?
The error you're receiving:
top: failed tty get
...happens when the top command isn't running in a real terminal session. The mode of ssh that Ansible uses doesn't support all the console features that the full blown terminal session would have - which is what top expects.

Resources