这几天运维的同事提了一个需求:生产的监控平台cacti+nagios目前通过网络上流行的飞信接口对接,当服务器有问题,会及时发送短信通知。但是用飞信发短信很不稳定,几乎每天都需要换校验码才行。于是提出将nagios监控报警发送短信接入公司自己的短信平台。
简单说下思路:1、原飞信都是在commands.cfg配置shell脚本执行的,我们可以沿用原来的消息格式;
2、短信平台可以开放一个URL调用接口,通过shell调用;
3、Linux中有curl指令,可以通过其使用post方式调用短信平台的Servlet;
4、于是,说干就干。。。。。
1、修改/usr/local/nagios/etc/objects/commands.cfg文件
################
#####
#####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
}
2、短信平台的web.xml里面增加发送短信Servlet的配置
<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>
3、开发一个发送短信的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);
}
……
4、重启短信平台,重启nagios,搞定~!
来源:oschina
链接:https://my.oschina.net/u/267081/blog/145224