当前位置: 技术问答>linux和unix
Linux实时发信功能,高手救我!
来源: 互联网 发布时间:2015-08-25
本文导语: 问题:在LINUX平台下实时监控MySQL数据库,如果数据库满足一定条件,就发自动发送Mail. 请高手指点一下具体的实现方法. 小弟不会忘了你的大恩大德. | #include #include #include #define MYSQL_ADMIN "tengulre@sohu...
问题:在LINUX平台下实时监控MySQL数据库,如果数据库满足一定条件,就发自动发送Mail.
请高手指点一下具体的实现方法.
小弟不会忘了你的大恩大德.
|
#include
#include
#include
#define MYSQL_ADMIN "tengulre@sohu.com"
void message_to_mail(char* mailto)
{
FILE * pipe_fp;
char buf[256];
sprintf(buf,"sendmail -t -i %s >/dev/null 2>&1",mailto);
pipe_fp = popen(buf,"w");
if(pipe_fp == NULL)
{
printf("can not open pipe %s",mailto);
exit(1);
}
fprintf(pipe_fp,"Subject: Monitor MySQL Servern");
fprintf(pipe_fp,"To: %sn",mailto);
fprintf(pipe_fp,"From: root(MySQL Administrator)n");
fprintf(pipe_fp,"n");
fprintf(pipe_fp,"Some Message about MySQL server statusn");
pclose(pipe_fp);
}
int main(int argc,char** argv)
{
/* 监视mysql */
while(TRUE)
{
if (!strcmp(FindSomeMessage(...),"NotThing"))
{
if(fork() == 0)
message_to_mail(MYSQL_ADMIN);
}
sleep(40);
}
exit(0);
}
#include
#include
#define MYSQL_ADMIN "tengulre@sohu.com"
void message_to_mail(char* mailto)
{
FILE * pipe_fp;
char buf[256];
sprintf(buf,"sendmail -t -i %s >/dev/null 2>&1",mailto);
pipe_fp = popen(buf,"w");
if(pipe_fp == NULL)
{
printf("can not open pipe %s",mailto);
exit(1);
}
fprintf(pipe_fp,"Subject: Monitor MySQL Servern");
fprintf(pipe_fp,"To: %sn",mailto);
fprintf(pipe_fp,"From: root(MySQL Administrator)n");
fprintf(pipe_fp,"n");
fprintf(pipe_fp,"Some Message about MySQL server statusn");
pclose(pipe_fp);
}
int main(int argc,char** argv)
{
/* 监视mysql */
while(TRUE)
{
if (!strcmp(FindSomeMessage(...),"NotThing"))
{
if(fork() == 0)
message_to_mail(MYSQL_ADMIN);
}
sleep(40);
}
exit(0);
}
|
当要发mail时起一个线程,主进程继续监控DB