sensu mailer and pipe - mailer

i'm switching over from nagios to sensu. i'm using chef to automated the process. everything is working great except the mailer or actually, i narrowed it down to the "pipe" that is suppose to redirect the json output from the check to the handler. it doesn't. when i use
{
"handlers": {
"email": {
"type": "pipe",
"command": "mail -s \"sensu alert\" alert#example.com",
"severities": [
"ok",
"critical"
]
}
}
}
i get a blank email. when i use the mailer.rb handler, i get no email whatsoever. i made sure to include mail to and mail from in the mailer.json. i see the logs have the correct information for the handler and email parameters.
so i've concluded the "pipe" isn't working. can anybody help with that? i would greatly appreciate it. i wish there was a sensu community, but it may be too new to have one.

With regards to the mailer.rb, have you checked the server logs (by default in /var/log/sensu/sensu-server.log) for errors? If there is an error in any of the handlers, they will show up in those logs.
mailer.rb requires several gems in order to run. To find out if you are using sensu's embedded ruby or not, check /etc/default/sensu for EMBEDDED_RUBY. If that is false, you will need to make sure your system ruby has all those gems (sensu-handler, mail, timeout) installed. If it is set to true, do the same with sensu's embedded ruby:
/opt/sensu/embedded/bin/gem list
Make sure the gems are installed, try again, and check the sensu-server.log for errors.
If you have more issues, there is in fact a community - check out #sensu on Freenode.

You can write you own event data JSON and pass it through a PIPE as follows:
cat event.json | /opt/sensu/embedded/bin/ruby mailer.rb
The easiest way to get the event.json file is from the sensu-server.log.

To use mailer.rb you need your own mail server ! if you'll post sensu server logs i think i can help you.

I've done some testing and the mail into pipe does not with GNU mail/mailx (assume you're using Ubuntu or something?).
Two solutions:
1) install BSD mail:
sudo apt-get install bsd-mailx
2) Or modify the command slightly get mail to read from stdin you'll need to do something like:
{
"handlers": {
"email": {
"type": "pipe",
"command": " echo $(cat) > /tmp/mail.txt; mail -s \"sensu alert\" alert#example.com < /tmp/mail.txt"
}
}
}
The idea is normally that you read the event json from stdin within a scripting language and then pull out bits of the event.json that you want to send. The above will e-mail out the entire json file.

You can use sensu mailer handler. Please find below steps to setup:-
sensu-install -p sensu-plugins-mailer
apt-get install postifx
/etc/init.d/postfix start
cd /etc/sensu/conf.d/
when we install this plugin will get 3 ruby files.
This time we are using this file:- handler-mailer.rb
First we need to creat handler file in this location /etc/sensu/conf.d/ :-
vim handler-mailer.json
{
"mailer": {
"admin_gui": "http://127.0.0.1:3000/",
"mail_from": "localhost",
"mail_to": ["yourmailid-1","yourmailid-2"],
"smtp_address": "localhost",
"smtp_port": "25"
}
}
Now we need to create one mail handler file in this location /etc/sensu/conf.d/:-
{
"handlers": {
"mymailer": {
"type": "pipe",
"command": "/opt/sensu/embedded/bin/handler-mailer.rb",
"severities": [
"critical",
"unknown"
]
}
}
}
in above file handler name is mymailer we need to use this handler name in our checks.

Use bin/handler-mailer-mailgun.rb or bin/handler-mailer-ses.rb or bin/handler-mailer.rb
Example:
echo '{
"id": "ef6b87d2-1f89-439f-8bea-33881436ab90",
"action": "create",
"timestamp": 1460172826,
"occurrences": 2,
"check": {
"type": "standard",
"total_state_change": 11,
"history": ["0", "0", "1", "1", "2", "2"],
"status": 2,
"output": "No keepalive sent from client for 230 seconds (>=180)",
"executed": 1460172826,
"issued": 1460172826,
"name": "keepalive",
"thresholds": {
"critical": 180,
"warning": 120
}
},
"client": {
"timestamp": 1460172596,
"version": "1.0.0",
"socket": {
"port": 3030,
"bind": "127.0.0.1"
},
"subscriptions": [
"production"
],
"environment": "development",
"address": "127.0.0.1",
"name": "client-01"
} }' | /opt/sensu/embedded/bin/handler-mailer-mailgun.rb
Output:
mail -- sent alert for client-01/keepalive to your.email#example.com

Related

json-server returns 404 for specific URI

I use json-server and my codes are like below.
db.json
{
"en-boards": [
{
"id": 0,
"name": "News",
"uri": "/board/en-news"
},
{
"id": 1,
"name": "Politics",
"uri": "/board/en-politics"
}
],
"board-en-news-threads-latest": [
{
"id": 0,
"poster": null,
"zero-post": "This is a thread 0.",
"attachment-path": null,
"post-number": 100,
"utc-timestamp": "2022-01-01T00:00:00"
},
{
"id": 1,
"poster": null,
"zero-post": "This is a thread 1.",
"attachment-path": null,
"post-number": 100,
"utc-timestamp": "2022-01-01T00:00:00"
}
]
}
routes.json
{
"/api/*": "/$1",
"/api/en-boards": "en-boards",
"/api/board/en-news/threads/latest": "board-en-news-threads-latest"
}
I run json-server --watch db.json --routes routes.json --middlewares middleware.js --port 4000
When I run curl -i http://localhost:4000/api/en-boards, I get 200 OK, but when I run curl -i http://localhost:4000/en-news/threads/latest, I get 404 Not Found. wget and React app behave in the same way.
Environment
MacOS 13.0
Node.js 18.11.0
json-server 0.17.0
curl 7.84.0
wget 1.21.3
React 18.2.0
Thank you for read. Any help would be appreciated.
Have faced something similar before...
In my case, I needed something like base URLs (1) and endpoint paths (2) like the below -
{
"/api/board/*": "/$1",
"/api/*": "/$1",
"/en-boards": "/en-boards",
"/en-news/threads/latest": "/board-en-news-threads-latest"}
This would cater to the following endpoints being hit
http://localhost:3000/api/board/en-news/threads/latest
http://localhost:3000/api/en-news/threads/latest
http://localhost:3000/en-news/threads/latest
http://localhost:3000/api/board/en-boards
http://localhost:3000/api/en-boards
http://localhost:3000/en-boards
Note: for /api/board/en-news/threads/latest to route to /board-en-news-threads-latest, the order for the base URLs needs to be maintained (i.e.)
"/api/board/*": "/$1",
"/api/*": "/$1",
Hope this helps :)

PM2-health : can i use pm2-health module for sending email alerts/notifications?

I have a nodejs application which runs on pm2 and I need to be able to send email notifications whenever a crash/ restart occurs. My idea is to monitor the application for crashes and trigger a mail action from pm2-health. The documentation of pm2-health module is here but I'm unable to use it for sending email alerts. Can anyone explain how to use it for this purpose?
P.S: Also, it would be great if you could explain about SMTP configuration for gmail.(I have configured postfix to use gmail smtp according to this and it works fine for test gmail but doesn't work with pm2-health)
This is how I could get pm2-health working with my Gmail account:
Install pm2-health module:
pm2 install pm2-health
Open PM2 module config file:
vim ~/.pm2/module_conf.json
Update it with the Gmail account’s SMTP parameters:
{
"pm2-health": {
"smtp": {
"host": "smtp.gmail.com",
"port": 465,
"user": "EXAMPLE_sender#gmail.com",
"password": "PASSWORD",
"secure": true,
"disabled": false
},
"mailTo": "NOTIFICATION_RECIPIENT_EMAIL_ADDRESS",
"replyTo": "EXAMPLE_SENDER#gmail.com",
"events": [
"exit"
],
"exceptions": true,
"messages": true,
"messageExcludeExps": [],
"metric": {},
"metricIntervalS": 60,
"aliveTimeoutS": 300,
"addLogs": false,
"appsExcluded": [],
"snapshot": {
"url": "",
"token": "",
"auth": {
"user": "",
"password": ""
},
"disabled": false
}
},
"module-db-v2": {
"pm2-health": {}
}
}
Save and close it
Restart pm2-health:
pm2 restart pm2-health
Test it by restarting one of your PM2-managed Node processes. You should receive an email about that event.
For anyone trying to use with 2FA enabled Gmail, you need to use an App Password. More information here: https://support.google.com/accounts/answer/185833

PowerShell shell hangs when saving results to variable (Azure Pipelines List)

I am trying to save the results of an Azure CLI (az pipelines list) command to a variable, but the shell/script hangs.
If I run the command on its own, it works:
PS C:\> az pipelines list --project PROJECT_NAME --name PIPELINE_NAME --output json
This command is in preview. It may be changed/removed in a future release.
[
{
"authoredBy": {
# ...
},
"createdDate": "2019-12-07T00:08:03.620000+00:00",
"draftOf": null,
"drafts": [],
"id": 541,
"latestBuild": null,
"latestCompletedBuild": null,
"metrics": null,
"name": "PIPELINE_NAME",
"path": "\\",
"project": {
"abbreviation": null,
"defaultTeamImageUrl": null,
"description": null,
"id": "99a1b81a-ca3b-418a-86cf-0965eaba6dab",
"lastUpdateTime": "2019-12-13T20:54:20.28Z",
"name": "PROJECT_NAME",
"revision": 462,
"state": "wellFormed",
"url": "https://dev.azure.com/ORGANIZATION_NAME/_apis/projects/99a1b81a-ca3b-418a-86cf-0965eaba6dab",
"visibility": "private"
},
"quality": "definition",
"queue": {
"id": 501,
"name": "Azure Pipelines",
"pool": {
"id": 65,
"isHosted": true,
"name": "Azure Pipelines"
},
"url": "https://dev.azure.com/ORGANIZATION_NAME/_apis/build/Queues/501"
},
"queueStatus": "enabled",
"revision": 30,
"type": "build",
"uri": "vstfs:///Build/Definition/541",
"url": "https://dev.azure.com/ORGANIZATION_NAME/99a1b81a-ca3b-418a-86cf-0965eaba6dab/_apis/build/Definitions/541?revision=30"
}
]
PS C:\>
However, if I try to assign the results to a variable, the shell/script hangs instead:
PS C:\> $pipelines = az pipelines list --project PROJECT_NAME --name PIPELINE_NAME --output json
This command is in preview. It may be changed/removed in a future release.
And the cursor jumps to the character 61 position and just stays there forever.
What may be the cause of this behaviour? I feel like the preview warning is causing some trouble, but I was not sure how to suppress it.
Any insight is greatly appreciated.
Thanks!
Okay, this is going to sound odd, but this is a rendering issue only - the app hasn't hung at all, it just stopped the console from outputting correctly, including the prompt after the command finishes.
At the top of your script add the following:
$PSBackgroundColor = $Host.UI.RawUI.BackgroundColor
$PSForegroundColor = $Host.UI.RawUI.ForegroundColor
function Reset-Console {
$Host.UI.RawUI.BackgroundColor = $PSBackgroundColor
$Host.UI.RawUI.ForegroundColor = $PSForegroundColor
}
Then after running the command:
Reset-Console
This fixed the issue for me.
As mentioned by the previous answer this is due to the color output of the azure cli (In this case the warning text) messes up the terminal.
Since PR [Core] Knack adoption #12604 it is possible to disable coloring of output for Azure cli by setting the environment variable AZURE_CORE_NO_COLOR to True (o alternatively by setting the [core] no_color=True option in ~/.azure/config
I am using i successfully with version 2.14.2 of Azure CLI.
From the description of PR [Core] Knack adoption #12604
[Core] PREVIEW: Allow disabling color by setting AZURE_CORE_NO_COLOR
environment variable to True or [core] no_color=True config (#12601)

How to pass argument in packer provision script?

I am struggling to pass input parameter to packer provisioning script. I have tried various options but no joy.
Objective is my provision.sh should accept input parameter which I send during packer build.
packer build -var role=abc test.json
I am able to get the user variable in json file however I am unable to pass it provision script. I have to make a decision based on the input parameter.
I tried something like
"provisioners":
{
"type": "shell",
"scripts": [
"provision.sh {{user `role`}}"
]
}
But packer validation itself is failed with no such file/directory error message.
It would be real help if someone can help me on this.
Thanks in advance.
You should use the environment_vars option, see the docs Shell Provisioner - environment_vars.
Example:
"provisioners": [
{
"type": "shell"
"environment_vars": [
"HOSTNAME={{user `vm_name`}}",
"FOO=bar"
],
"scripts": [
"provision.sh"
],
}
]
If your script is already configured to use arguments; you simply need to run it as inline rather than in the scripts array.
In order to do this, the script must exist on the system already - you can accomplish this by copying it to the system with the file provisioner.
"provisioners": [
{
"type": "file",
"source": "scripts/provision.sh",
"destination": "/tmp/provision.sh"
},
{
"type": "shell",
"inline": [
"chmod u+x /tmp/provision.sh",
"/tmp/provision.sh {{user `vm_name`}}"]
}
]

AWS S3 permissions - error with put-bucket-acl

I am trying to move an S3 bucket from one account (A) to another (B).
I have succeeded with that operation and remove the bucket from account A.
I am trying to move the new bucket from account B to another bucket on account B, but learning that beside the bucket itself I have no access to the files.
After much fighting with s3 cli and its permissions I checked s3api commands and found out that the files (surprise surprise) still holds the old ownership.
I am trying now to change it, but came to a stand still with the put-bucket-acl, the JSON file isn't working for s3api command.
I tried running the command in debug , but didn't make too much out of it.
Anybody knows what to do ?
Maybe a better way to solve this issue ?
what I did so far:
the command:
aws s3api put-bucket-acl --bucket my-bucket --cli-input-json file://1.json
(Same with put-object-acl)
1.json file:
"Grantee": {
"DisplayName": "account_B",
"EmailAddress": "user#mail.com",
"ID": "111111hughalphnumericnumber22222",
"Type": "CanonicalUser",
"Permission": "FULL_CONTROL"
}
The errors I get :
Unknown parameter in input: "Grantee", must be one of: ACL,
AccessControlPolicy, Bucket, ContentMD5, GrantFullControl, GrantRead,
GrantReadACP, GrantWrite, GrantWriteACP Unknown parameter in input:
"Permission", must be one of: ACL, AccessControlPolicy, Bucket,
ContentMD5, GrantFullControl, GrantRead, GrantReadACP, GrantWrite,
GrantWriteACP
UPDATE:
AssumeRole between the 2 accounts doesn't work in my case.
cli (s3cmd,s3api) GUI (MCSTools,bucketexplorer), ACL using headers,body (Postman) did not help as well..
I'm connecting AWS support and hoping for the best.
I'll update when I have a solution.
So, AWS support came to the rescue... I'm leaving this for others to see, so they won't have to waste 2 days like I did trying to figure what the hell went wrong...
aws s3api get-object-acl --bucket <bucket_on_B> --key <Key_on_B_Owned_by_A> --profile IAM_User_A > A_to_B.json
apply the outcome of:
aws s3api get-bucket-acl --bucket <Bucket_on_B> --profile IAM_User_B
onto the json file that was created, and then run
aws s3api put-object-acl --bucket <Bucket_on_B> --key <Key_on_B_Owned_by_A> --access-control-policy file://A_to_B.json --profile IAM_User_A
Your JSON is wrong. According to the documentation for the put-bucket-acl option you can generate valid JSON template ('skeleton') using --generate-cli-skeleton. For example:
aws s3api put-bucket-acl --bucket BUCKETNAME --generate-cli-skeleton
And here is the output:
{
"ACL": "",
"AccessControlPolicy": {
"Grants": [
{
"Grantee": {
"DisplayName": "",
"EmailAddress": "",
"ID": "",
"Type": "",
"URI": ""
},
"Permission": ""
}
],
"Owner": {
"DisplayName": "",
"ID": ""
}
},
"Bucket": "",
"ContentMD5": "",
"GrantFullControl": "",
"GrantRead": "",
"GrantReadACP": "",
"GrantWrite": "",
"GrantWriteACP": ""
}
For anyone who's still looking to do this - OP probably looked at the right aws doc but overlooked the right command. I'm just glad I got to right command because of this stackoverflow page :)
https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-acl.html
^^ The json syntax with example is present there and instead of --cli-input-json , use --access-control-policy
{
"Grants": [
{
"Grantee": {
"DisplayName": "string",
"EmailAddress": "string",
"ID": "string",
"Type": "CanonicalUser"|"AmazonCustomerByEmail"|"Group",
"URI": "string"
},
"Permission": "FULL_CONTROL"|"WRITE"|"WRITE_ACP"|"READ"|"READ_ACP"
}
...
],
"Owner": {
"DisplayName": "string",
"ID": "string"
}
}
I had the policy as a json file and used this command it worked just fine.
aws s3api put-bucket-acl --bucket bucketname --access-control-policy file://yourJson.json
Also one more thing to note is that I wasn't able to add permissions along with existing ones, old acl was being overwritten. So any permission you want to add needs to be in json policy file along with existing policy. It will be easier when you use some command to describe all the ACLs first.
The syntax is the following (with example):
aws s3api put-bucket-acl --bucket bucket_name --access-control-policy file://grant.json
grant.json file:
{
"Grants": [
{
"Grantee": {
"ID": "CANONICAL_ID_TO_GRANT",
"Type": "CanonicalUser"
},
"Permission": "WRITE"
},
{
"Grantee": {
"ID": "CANONICAL_ID_TO_GRANT",
"Type": "CanonicalUser"
},
"Permission": "READ"
}
],
"Owner": {
"DisplayName": "example_owner",
"ID": "CANONICAL_ID_OWNER"
}
}