Nagios呼叫WSDL WebServer介面傳送簡訊

sundog315發表於2014-05-29

使用JAVA呼叫簡訊閘道器WebService


import java.text.SimpleDateFormat;
import org.apache.commons.lang.StringEscapeUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;

public class AddSMSList {
       public static void main(String[] args) {
              try {
                     String endpoint = "";
                     Service service = new Service();
                     Call call = (Call) service.createCall();
                     call.setTargetEndpointAddress(new java.net.URL(endpoint));
                     call.setOperationName(new QName(endpoint, "AddSMSList"));            
       call.addParameter("validation", org.apache.axis.encoding.XMLType.XSD_STRING,
                                   javax.xml.rpc.ParameterMode.IN);
                     call.addParameter("XML", org.apache.axis.encoding.XMLType.XSD_STRING,
                                   javax.xml.rpc.ParameterMode.IN);
       call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
                     String valid = "POWERU-SMS";

       SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
       String dt = sdf.format(new Date());
       dt = dt.substring(0,16);
       dt = dt + "0";
       //System.out.println(dt);
   
       String ar = args[1];
       String content = StringEscapeUtils.escapeHtml(ar);
       //System.out.println(content);

       String xmlbody = "";  //簡訊介面格式
       //System.out.println(xmlbody);

       String result = (String)call.invoke(new Object[]{valid, xmlbody});
       System.out.println("result is "+result);

       File tofile=new File("/usr/local/nagios/var/sms.log"); 
       FileWriter fw=new FileWriter(tofile,true); 
       BufferedWriter bw=new BufferedWriter(fw); 
       PrintWriter pw=new PrintWriter(bw); 

       pw.println(dt + ":" + xmlbody + ":" + result);  //記錄日誌
       pw.close(); 
       bw.close(); 
       fw.close();
              }
              catch (Exception e) {
                     System.err.println(e.toString());
              }
       }
}


編譯

javac -classpath .:./javaee.jar:./axis-1.4.jar:./commons-logging-1.1.1.jar:./commons-discovery-20040218.194635.jar:./wsdl4j-1.6.2.jar:./javax.activation.jar:./javax.mail-1.3.3.01.jar:./commons-lang-2.4.jar AddSMSList.java


傳送指令碼

sendSMS.sh

export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin

LOCAL_PATH=$(cd "$(dirname "$0")"; pwd)

java -classpath $LOCAL_PATH:$LOCAL_PATH/javaee.jar:$LOCAL_PATH/axis-1.4.jar:$LOCAL_PATH/commons-logging-1.1.1.jar:$LOCAL_PATH/commons-discovery-20040218.194635.jar:$LOCAL_PATH/wsdl4j-1.6.2.jar:$LOCAL_PATH/javax.activation.jar:$LOCAL_PATH/javax.mail-1.3.3.01.jar:$LOCAL_PATH/commons-lang-2.4.jar AddSMSList "$1" "$2"

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/19423/viewspace-1172565/,如需轉載,請註明出處,否則將追究法律責任。

相關文章