這幾天運維的同事提了一個需求:生產的監控平臺cacti+nagios目前經過網絡上流行的飛信接口對接,當服務器有問題,會及時發送短信通知。可是用飛信發短信很不穩定,幾乎天天都須要換校驗碼才行。因而提出將nagios監控報警發送短信接入公司本身的短信平臺。 html
簡單說下思路:一、原飛信都是在commands.cfg配置shell腳本執行的,咱們能夠沿用原來的消息格式;
二、短信平臺能夠開放一個URL調用接口,經過shell調用;
三、Linux中有curl指令,能夠經過其使用post方式調用短信平臺的Servlet;
四、因而,說幹就幹。。。。。 java
一、修改/usr/local/nagios/etc/objects/commands.cfg文件 ios
################
#####
#####fetion
#####
#####
###########a#####
define command{
command_name host-notify-by-fx
command_line curl -d "orgid=0001&userid=test&msg=Host $HOSTSTATE$ alert for $HOSTNAME$! on $LONGDATETIME$&telno=$CONTACTPAGER$" http://127.0.0.1:8080/sms/sendSms
}
###########
####
####
####
##########
define command{
command_name service-notify-by-fx
command_line curl -d "orgid=0001&userid=test&msg=Host $HOSTSTATE$ alert for $HOSTNAME$! on $LONGDATETIME$&telno=$CONTACTPAGER$" http://127.0.0.1:8080/sms/sendSms
} web
二、短信平臺的web.xml裏面增長髮送短信Servlet的配置 shell
<servlet>
<servlet-name>sendSms</servlet-name>
<servlet-class>com.cpp.smsapp.connect.SendSmsServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sendSms</servlet-name>
<url-pattern>/sendSms</url-pattern>
</servlet-mapping> apache
三、開發一個發送短信的Servlet,叫SendSmsServlet.java 服務器
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.cpp.sms.model.dao.SmsSendLogDAO;
import com.cpp.sms.model.dao.daoImpl.SignInfoDAOImpl;
import com.cpp.sms.model.dao.daoImpl.SmsSendLogDAOImpl;
import com.cpp.sms.model.dao.daoImpl.UserInfoDAOImpl;
import com.cpp.sms.model.entity.OrgBean;
import com.cpp.sms.model.entity.SignInfoBean;
import com.cpp.sms.model.entity.SmsSendLogBean;
import com.cpp.sms.model.entity.UserInfoBean;
import com.cpp.sms.util.GetOrgInfoFactory;
import com.cpp.sms.util.Tools;
import com.dc.card.clientwebapp.config.AppConfig;
public class SendSmsServlet extends javax.servlet.http.HttpServlet {
private static final long serialVersionUID = 7933467409474273978L;
static Log log = LogFactory.getLog(SendSmsServlet.class);
public void doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) {
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8");
PrintWriter resTools = null;
try {
resTools = response.getWriter();
} catch (IOException e1) {
if (log.isErrorEnabled()) {
log.error("得到Writer失敗");
}
resTools.print("-1");
return;
}
String orgid = request.getParameter("orgid");
String userid = request.getParameter("userid");
String pwdStr = request.getParameter("pwd");
Base64 base64 = new Base64();
// base64解碼
try {
byte[] debytes = base64.decode(pwdStr.getBytes("utf-8"));
pwdStr = new String(debytes, "utf-8");
} catch (UnsupportedEncodingException e1) {
if (log.isErrorEnabled()) {
log.error("不支持的編碼格式");
}
resTools.print("-1");
return;
}
if (log.isDebugEnabled()) {
log.debug("base64解碼=" + pwdStr);
}
網絡
…… app
四、重啓短信平臺,重啓nagios,搞定~! 運維