Chinaunix首页 | 论坛 | 博客

qsh

  • 博客访问: 3943623
  • 博文数量: 1015
  • 博客积分: 15904
  • 博客等级: 上将
  • 技术积分: 8572
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-04 19:16
文章分类

全部博文(1015)

文章存档

2019年(1)

2017年(1)

2016年(19)

2015年(27)

2014年(30)

2013年(95)

2012年(199)

2011年(72)

2010年(109)

2009年(166)

2008年(296)

分类:

2012-06-29 22:00:56

原文地址:短信监控报警 作者:ospcn


短信监控报警



调试的步骤如下:
1. cd /usr/src/
wget
tar zxvf smstools-2.2.18.tar.gz
cd smstools
make
make install

cd /etc/rc.d/rc3.d
ln -s ../init.d/sms S99sms

安装smstools,主要的程序安装在/usr/local/bin下面,开机时自动启动smsd。
smstools用来收发短信的,收到的放在/var/spool/sms/incoming目录。
如果要发短信,按照一定的格式写一个文件到/var/spool/sms/outgoing目录就可以了。
文件的格式在,但是发中文的话,必
须编码。

2. 我写的一个编码程序
编译产生 /var/spool/sms/st
这个程序是发送短信的,命令行是
/var/spool/sms/st 电话号码 短信内容

比如执行
/var/spool/sms/st 13505693311 test
就发送“test"给13505693311
注意,后面的短信内容不能有空格或者引起命令行特殊处理的字符,比如&|><等

3. 成组发
为了给一组手机发短信,可以在/var/spool/sms/group下建立一个文件,文件的内容是多个手
机号码
是给一组用户发的,比如
/var/spool/sms/stg all test
会把"test"发给/var/spool/sms/group/all文件中列出的号码
为了方便,如果/var/spool/sms/group/13505693311不存在,
执行/var/spool/sms/stg 13505693311 test
则发给1350569311

同样注意,后面的短信内容不能有空格或者引起命令行特殊处理的字符,比如&|><等

4. 简单的ping监视程序
在/usr/src/monitor/目录下,是辅助程序
在/usr/src/monitor/ping/目录下,是主程序,pinglist是配置文件
pinglist的格式为
IP地址 描述 短信组或手机号

run执行时,如果连续的3次运行,都ping不通 IP地址,就发送给手机提示。
一旦ping通了,就再发一次提示。


/* 发送短信程序
将程序给出的短信编码后放到smstools的outgoing目录
长短信的处理:
单条短信为70个汉字,注意:1个英文字符算1个汉字长度
因此<=70汉字的短信1条就可以了
多于70汉字的短信,需要拆分,拆分时,每条65字符,前面增加[y/x]标记
表明是总x段的第y个,其中x,y均为单个数字,因此最长短信为65*9=641个汉字
多的汉字会被忽略
*/

#include
#include
#include
#include
#include
#include
#include

#define BUFLEN 1024
#define DEBUG 1
#define OUTGOING "/var/spool/sms/outgoing/"

//GB2312码转为UNICODE码
int g2u(char *inbuf,char *outbuf)
{
iconv_t cd;
int i;
unsigned char oout[BUFLEN];
char **pin, **pout;
char *saved_outbuf=outbuf;
int outlen=BUFLEN;
cd = iconv_open("unicode","gb2312");
if (cd==0) perror("iconv_open");
memset(outbuf,0,outlen);

while(*inbuf) {
if((inbuf[0]&0x80)) { //GB2312
int a=2;
char *tout=oout;
pin=&inbuf;
pout=&tout;
if (iconv(cd,pin,&a,pout,&outlen)==-1) perror("iconv");
if((oout[0]==0xff) && (oout[1]==0xfe)) {
outbuf[0]=oout[3];
outbuf[1]=oout[2];
} else {
outbuf[0]=oout[1];
outbuf[1]=oout[0];
}
i++;
outbuf+=2;
} else {
outbuf[0]=0;
outbuf[1]=inbuf[0];
outbuf+=2;
inbuf++;
}
}
iconv_close(cd);
return outbuf-saved_outbuf;
}

int sendsms(char *pnum, char *msg, int index)
{
char fname[BUFLEN];
char inbuf[BUFLEN];
char outbuf[BUFLEN];
FILE *fp;
int r;
snprintf(fname,BUFLEN,"/tmp/sms.%ld.%d.%d",time(NULL),getpid(),index);
fp=fopen(fname,"w");
if(fp==NULL) {
perror(fname);
exit(0);
}
fprintf(fp,"To: +86%s\n",pnum);
fprintf(fp,"Alphabet: UCS\n\n");
snprintf(inbuf,BUFLEN,"%s",msg);
r=g2u(inbuf,outbuf);
fwrite(outbuf,1,r,fp);
fclose(fp);
snprintf(inbuf,BUFLEN,"mv %s %s/",fname,OUTGOING);
system(inbuf);
return 0;
}

void usage(void)
{
printf("st: pho_num msg\n");
exit(0);
}

int main(int argc, char *argv[])
{ int len,index,totalindex;
char *p,*p2;
char msgbuf[BUFLEN];
if(argc!=3) usage();
len=0; //计算有多少个汉字,一条短信只能发送70个汉字,一个英文字符算一个汉字
p=argv[2];
while(*p) {
if((*p)&0x80) //GB2312
p+=2;
else
p++;
len++;
}
#ifdef DEBUG
printf("%s: %d len\n",argv[2],len);
#endif
if(len<=70) { // <= 70汉字的直接发送
sendsms(argv[1],argv[2],1);
exit(0);
}
totalindex=(len-1)/65+1;
index=1;
p=argv[2];
while(*p) {
if(index==10) break; //最多发送9段,多于的忽略
#ifdef DEBUG
printf("%d/%d:",index,totalindex);
#endif
len=0;
sprintf(msgbuf,"[%d/%d]",index,totalindex);
p2=msgbuf+5;
while(*p) {
if((*p)&0x80) { //GB2312
*p2=*p;
*(p2+1)=*(p+1);
p+=2;
p2+=2;
} else {
*p2=*p;
p++;
p2++;
}
len++;
if(len==65) break;
}
*p2=0;
#ifdef DEBUG
printf("%s\n",msgbuf);
#endif
sendsms(argv[1],msgbuf,index);
index++;
}

}


#!/bin/sh
if [ -f /var/spool/sms/group/$1 ] ; then
for n in `cat /var/spool/sms/group/$1`; do /var/spool/sms/st $n $2; done
else
/var/spool/sms/st $1 "$2"
fi




#!/bin/sh

log ()
{ a=`date`;
echo $a $1 >> /usr/src/monitor/log.txt
# echo " " $1 >> /usr/src/monitor/log.txt
}



#!/bin/sh
. /usr/src/monitor/functions

#DEBUG=1

COUNT=3

while read ip name UG
do

ALFILE=/usr/src/monitor/ping/$ip.alarmon
if [ $DEBUG ]; then echo $ip; fi

ping -w 2 $ip > /dev/null
tmp=$?
if [ $DEBUG ]; then echo $tmp; fi

if test $tmp == 0 ; then
if test -f $ALFILE.count ; then
rm -f $ALFILE.count
fi
if test -f $ALFILE ; then
msg="ping_${name}_${ip}正常"
log $msg
if [ $DEBUG ]; then echo $msg; fi
/var/spool/sms/stg $UG $msg
rm -f $ALFILE
fi
else
if ! test -f $ALFILE ; then
if test -f $ALFILE.count; then
count=`cat $ALFILE.count`
else
count=0
fi
if test $count -gt $COUNT; then
msg="ping_${name}_${ip}异常"
log $msg
if [ $DEBUG ]; then echo $msg; fi
/var/spool/sms/stg $UG $msg
touch $ALFILE
else
echo `expr $count + 1` > $ALFILE.count
fi
fi
fi
done < /usr/src/monitor/ping/pinglist

阅读(803) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~