Giter Site home page Giter Site logo

pm2-slack's Introduction

pm2-slack

This is a PM2 Module for sending events & logs from your PM2 processes to Slack.

Install

To install and setup pm2-slack, run the following commands:

pm2 install pm2-slack
pm2 set pm2-slack:slack_url https://slack_url

To get the Slack URL, you need to setup an Incoming Webhook. More details on how to set this up can be found here: https://api.slack.com/incoming-webhooks

Events subscription configuration

The following events can be subscribed to:

  • log - All standard out logs from your processes. Default: false
  • error - All error logs from your processes. Default: true
  • kill - Event fired when PM2 is killed. Default: true
  • exception - Any exceptions from your processes. Default: true
  • restart - Event fired when a process is restarted. Default: false
  • reload - Event fired when a cluster is reloaded. Default: false
  • delete - Event fired when a process is removed from PM2. Default: false
  • stop - Event fired when a process is stopped. Default: false
  • restart overlimit - Event fired when a process is reaches the max amount of times it can restart. Default: true
  • exit - Event fired when a process is exited. Default: false
  • start - Event fired when a process is started. Default: false
  • online - Event fired when a process is online. Default: false

You can simply turn these on and off by setting them to true or false using the PM2 set command.

pm2 set pm2-slack:log true
pm2 set pm2-slack:error false

Options

The following options are available:

  • slack_url (string) - Slack Incomming Webhook URL.
  • servername / username (string) - Set the custom username for Slack messages (visible in message headers). Default: server hostname
  • buffer (bool) - Enable/Disable buffering of messages. Messages that occur in short time will be concatenated together and posted as a single slack message. Default: true
  • buffer_seconds (int) - If buffering is enables, all messages are stored for this interval. If no new messages comes in this interval, buffered message(s) are sended to Slack. If new message comes in this interval, the "timer" will be reseted and buffer starts waiting for the new interval for a new next message. Note: Puspose is reduction of push notifications on Slack clients. Default: 2
  • buffer_max_seconds (int) - If time exceed this time, the buffered messages are always sent to Slack, even if new messages are still comming in interval (property buffer_seconds). Default: 20
  • queue_max (int) - Maximum number of messages, that can be send in one Slack message (in one bufferring round). When the queue exceeds this maximum, next messages are suppresesed and replaced with message "Next XX messages have been suppressed.". Default: 100

Set these options in the same way as subscribing to events.

Example

The following configuration options will enable message buffering, and set the buffer duration to 5 seconds. All messages that occur within maximum 5 seconds delay between two neighboring messages will be concatenated into a single slack message.

pm2 set pm2-slack:slack_url https://hooks.slack.com/services/123456789/123456789/aaaaaaa
pm2 set pm2-slack:buffer_seconds 5

Note: In this example, the maximum total delay for messages is still 20 seconds (default value for buffer_max_seconds). After this time, the buffer will be flushed everytime and all messages will be sent.

Process based custom options

By default, all options are defined for all processes globally. But you can separately define custom options to each PM2 process. Use format pm2-slack:optionName-processName to process based custom options.

If no custom options is defined, the global pm2-slack:propertyName will be used.

Note: By this way, all custom options can be used for specific process, but events subsciptions configuration is always global only.

Example

We have many processes, includes process foo and process bar. For this two processes will have to define separate Slack URL channel and separate server name. Same buffer options will be used for all processed.

# Define global options for all processes.
pm2 set pm2-slack:buffer_seconds 5

# Define global options for all processes.
#   (for process `foo` and `bar` the values will be overridden below).
pm2 set pm2-slack:slack_url https://hooks.slack.com/services/123456789/123456789/aaaaaaa
pm2 set pm2-slack:servername Orion

# Define custom Slack Incomming Webhoook for `foo` process.
pm2 set pm2-slack:slack_url-foo https://hooks.slack.com/services/123456789/123456789/bbbbbbb
pm2 set pm2-slack:servername-foo Foo-server
# Note: The `pm2-slack:buffer_seconds`=5 will be used from global options for this process. 

# Define custom Slack Incomming Webhoook for `bar` process
pm2 set pm2-slack:slack_url-bar https://hooks.slack.com/services/123456789/123456789/ccccccc
pm2 set pm2-slack:servername-foo Bar-server
# Note: The `pm2-slack:buffer_seconds`=5 will be used from global options for this process. 

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Release History

  • 1.1.0 Custom options can be defined to each PM2 process. Displaying process ID of cluster mode processes (thanks @abawchen).
  • 1.0.0 Message bufferring refactored. Message grouping refactored. Added datetime parsing from log messages.
  • 0.3.4 Added an option to override the Slack username
  • 0.3.3 Added documentation for the reload event
  • 0.3.2 Fixed Half width of error and log messages (thanks @ma-zal)
  • 0.3.1 Fixed Double escaping of error and log messages (thanks @ma-zal)
  • 0.3.0 Switched to a default buffer system that groups alike messages by timestamp in the same message to Slack (thanks @kjhangiani)
  • 0.2.0 Implemented a rate limiting system and updated all the dependencies
  • 0.1.1 Commenting & Clean up
  • 0.1.0 Initial Release

pm2-slack's People

Contributors

abawchen avatar kjhangiani avatar ma-zal avatar matkinson-godaddy avatar mattpker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pm2-slack's Issues

Events not being fired

I have used it to monitor my apps. But the thing is that it is not properly emitting the events. Restarting an app doesn't emit restart event , instead, it emits two exit events. Anybody got an idea why it might be happening?

Usage of old pm2 version

Is there a reason this package uses version 0.15.10 of pm2? It is at 2.7.2 now.
The reason I ask is since the old version has a ikt: 'git+http://ikt.pm2.io/ikt.git#master' dependency which is messing with our builds and is gone in the newer version

Allow custom messages and notifications

Thanks for great project! I have following feature request:
I want to add <!channel> to messages, so all members in our Slack channel get notification.

Preferably as option, so we can ping only specific user per process.

error on install `Unknown module`

Hello

first, thank you for your wonderful work!

I got this error on my gliderlabs/alpine:3.3 image

log:

$ pm2 install pm2-slack
[PM2][Module] Installing module pm2-slack
[PM2][Module] Module already installed. Updating.
[PM2][Module] Processing...
............/root/.pm2
`-- [email protected]

[PM2][Module][ERROR] Unknown module

Memory usage

First of all, thanks for this module :)

I'm experiencing some weird memory usage on my system.

image

120MB for such a tiny module seems a bit odd, what are your thoughts on this? I didn't have time to debug this yet but maybe you or someone else already have some insights.

error with pm2 2.5.0

`/usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:130
return typeof obj == 'object' && obj.width != undefined
^

TypeError: Cannot read property 'width' of null
at get_width (/usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:130:41)
at /usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:125:68
at Array.forEach (native)
at extractColumnWidths (/usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:124:9)
at /usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:98:11
at Array.forEach (native)
at Table.toString (/usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:95:16)
at /usr/lib/node_modules/pm2/lib/API/CliUx.js:329:29
at Array.forEach (native)
at Object.UX.dispKeys (/usr/lib/node_modules/pm2/lib/API/CliUx.js:310:19)`

Multiple slack_url

Hi, is there a way to configure 2 or more slack webhook urls? I have two channels and I need to set two webhooks or at least set different channels from PM2 module.

Maybe it's done but i didn't find it.

Thanks in advance.

Add id to title

Would it be possible to add the id of the process to the message title?
If you have 4 processes with the same name this helps to see where an event happened.

How can I strip terminal color code

Hello.

My application is outputting logs using winston with colorize option set to true. This allows me to see logs that is colorized based on the log type (error shows up in red, etc..)

Now, since pm2-slack sends logs to slack without stripping the terminal color code, I am seeing something like following in the slack channel.

my.host.com BOT [3:44 AM]  
sca-wf-task - error
----------------
"Thu Oct 06 2016 07:44:01 GMT+0000 (UTC) - \u001b[31merror\u001b[39m: can't clean taskdir on resource_id:575ef81e9b8f34d642b15106 because resource status is not ok\n"

Is there anyway to strip those terminal color codes like.. \u001b[31m?

Configuration per process ?

I am not familiar with PM2 plugin architecture, but is it possible to somehow enable this plugin only for a particular process? On our server there are some processed which are not production related so we don't care about those much.

Increase buffer seconds limit

Is there a reason why the maximum buffer second limit is only 5 seconds? I have a usecase that I need it to send only every 10/30 seconds.

App specific log events and regex specific log string match

I have a node application for downloading some files daily in that application it shows the progress in the command but i want only specific log messages to be notified when the specified regex is matched to the log name or description. Also i want to notify only for specific app process because pm2 can be processed for many apps

Is it a bug in cluster mode?

I have an app running in cluster mode that every time I restart it, there would be two null messages as follows:

screen shot 2017-11-17 at 3 05 55 pm

pm2 reload

Is there a way to notify on a reload? Either from pm2 reload or due to the --max-memory-restart flag?

Allow custom username

Currently you are using os.hostname() as username for slack messages.
As we don't want to make it public, could you add an option to override it?
We have also tried setting a default on the Slack webhook but that gets ignored.
Thanks

Error when I try to install

Refusing to download the git repo

{"protocol":"http:","hostname":"ikt.pm2.io","repository":"http://ikt.pm2.io/ikt.git"} over HTTP without a commit hash
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

Environment:
DigitalOcean, Alpine Linux (inside the docker container)

Warning [email protected]: The platform "linux" is incompatible with this module

Hi Matt,

Since the latest install, we get this strange warning and error.

We are using Yarn to install all our packages.
But it seems to be related to something in the pm2-slack module.

StdOut:
[PM2][Module] Installing module pm2-slack
[PM2][Module] Module already installed. Updating.
[PM2] Applying action deleteProcessId on app [0](ids: 0)
[PM2] [pm2-slack](0) ✓
In memory process deleted
[PM2][Module] Calling [YARN] to install pm2-slack ...
yarn add v0.24.5
[1/4] Resolving packages...
[2/4] Fetching packages...
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved 1 new dependency.
└─ [email protected]
Done in 1.02s.
[PM2][Module] Module downloaded
[PM2][WARN] Applications index not running, starting...
[PM2] App [pm2-slack] launched (1 instances)
== pm2-slack ==

StdErr:
warning No license field
warning [email protected]: The platform "linux" is incompatible with this module.
warning No license field
/usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:130
    return typeof obj == 'object' && obj.width != undefined
                                        ^

TypeError: Cannot read property 'width' of null
    at get_width (/usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:130:41)
    at /usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:125:68
    at Array.forEach (native)
    at extractColumnWidths (/usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:124:9)
    at /usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:98:11
    at Array.forEach (native)
    at Table.toString (/usr/lib/node_modules/pm2/node_modules/cli-table/lib/index.js:95:16)
    at /usr/lib/node_modules/pm2/lib/API/CliUx.js:329:29
    at Array.forEach (native)
    at Object.UX.dispKeys (/usr/lib/node_modules/pm2/lib/API/CliUx.js:310:19)

slack message not formal.

hello. i'm using pm2-slack module.

slack message not formal.

ex.

"2017-02-16T10:00:00.729Z - \u001b[31merror\u001b[39m: [ auth/email-already-in-use ] The email address is already in use by another account. \n{ email: \u001b[32m'[email protected]'\u001b[39m,\n password: \u001b[32m'barogo1004!'\u001b[39m,\n phone: \u001b[32m'0111111242'\u001b[39m }\n"

why print this message?

this module verfy useful to me..

thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.