Integrate WeChat Alerts into Nagios Using Node.js
This guide walks you through registering a WeChat public account, setting up a Node.js simulator for login and friend retrieval, installing Node.js, configuring Nagios commands and contacts, and finally enabling WeChat notifications for monitoring alerts.
With the rapid growth of mobile internet, WeChat is widely used and can be integrated into monitoring systems. This article explains how to add WeChat alerting to Nagios.
1. Register a WeChat public account
Visit https://mp.weixin.qq.com/ and create a public account (subscription or service account).
2. Set up the Node.js simulator
Download the Node.js program and edit config/config.coffee with the account credentials.
The program provides two functions: simulate login and fetch the friend list.
2.1 Retrieve friend information
Access http://IP:8080/wx/friend to get a JSON array of friends. Example output:
{
"id": 5726745,
"nick_name": "user1",
"remark_name": "",
"group_id": 0
},
{
"id": 618272308,
"nick_name": "user2",
"remark_name": "",
"group_id": 0
},
{
"id": 2026561000,
"nick_name": "user3",
"remark_name": "",
"group_id": 0
}3. Install Node.js
wget http://nodejs.org/dist/v0.10.26/node-v0.10.26-linux-x64.tar.gz
tar xvzf node-v0.10.26-linux-x64.tar.gz
cp -Rp node-v0.10.26 /usr/local/
export PATH=$PATH:/usr/local/node-v0.10.26/bin
# or use nvm for easier installation4. Run the interface program
cd node_wx
node . # start the Node.js serviceYou can run it with pm2 start index.js and configure it to start on boot.
5. Create a shell script to call the WeChat API
Place the script (e.g., weixin.sh) in /usr/local/bin/. The archive weixin.rar contains a sample implementation.
6. Configure Nagios
6.1 Define notification commands (objects/commands.cfg)
define command{
command_name notify-host-by-weixin
command_line /usr/bin/printf "%b" "*****ERS monitor Robot*****
Notification Type: $NOTIFICATIONTYPE$
Host: $HOSTNAME$
State: $HOSTSTATE$
Address: $HOSTADDRESS$
Info: $HOSTOUTPUT$
Date/Time: $LONGDATETIME$
" > /usr/local/nagios/var/weixin.out | /usr/local/bin/weixin.sh -f $CONTACTADDRESS1$
}
define command{
command_name notify-service-by-weixin
command_line /usr/bin/printf "%b" "***** ERS monitor Robot*****
Notification Type: $NOTIFICATIONTYPE$
Service: $SERVICEDESC$
Host: $HOSTALIAS$
Address: $HOSTADDRESS$
State: $SERVICESTATE$
Date/Time: $LONGDATETIME$
Additional Info:
$SERVICEOUTPUT$
" > /usr/local/nagios/var/weixin.out | /usr/local/bin/weixin.sh -f $CONTACTADDRESS1$
}6.2 Define a contact that includes the WeChat command (objects/contacts.cfg)
define contact{
contact_name nagiosadmin
alias Nagios Admin
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-host-by-email,notify-host-by-sms,notify-service-by-weixin
host_notification_commands notify-host-by-email,notify-host-by-sms,notify-service-by-weixin
pager 134123456
email [email protected]
address1 2447905640 ; WeChat friend ID
}After saving the configuration, restart Nagios and test the setup. Now Nagios can send alerts to WeChat.
Note: The author’s knowledge is limited; corrections are welcome.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
