|
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <mbstring.h>
#include <winsock2.h>
#include "pingoo.h"
typedef int (* LPSendMail)
(
LPCSTR szServer,
LPCSTR szTo,
LPCSTR szFrom,
LPCSTR szSubject,
LPCSTR szBody,
LPCSTR szFile,
LPSTR msg
);
typedef PINGINFO (* LPSendEchoRequest)
(
const char* host
);
char buff[128];
HINSTANCE Lib1;
HINSTANCE Lib2;
LPSendMail funcSendMail;
LPSendEchoRequest funcSendEchoRequest;
PINGINFO info;
int ret;
char SmtpServer[128];
int nRetry,nRetryTime;
char szTo[80];
char szToAdmin[80];
char szFrom[80];
char szSubject[80];
char szBody[4096];
char szBodyAdmin[4096];
char szErrorMessage[80];
int main(int argc, char* argv[])
{
FILE *fp;
int nIdx;
char *tk1,*tk2;
fp = fopen( "host_chk.lst", "rt" );
if ( fp == NULL ) {
return 0;
}
nIdx = 0;
while( 1 ) {
fgets( buff, 128, fp );
if ( buff[0] == ';' ) {
continue;
}
if ( feof( fp ) ) {
break;
}
nIdx++;
buff[lstrlen(buff)-1] = 0x00;
if ( nIdx == 1 ) {
lstrcpy( SmtpServer, buff );
continue;
}
if ( nIdx == 2 ) {
nRetry = atoi( buff );
continue;
}
if ( nIdx == 3 ) {
nRetryTime = atoi( buff );
continue;
}
if ( nIdx == 4 ) {
lstrcpy( szTo, buff );
continue;
}
if ( nIdx == 5 ) {
lstrcpy( szToAdmin, buff );
continue;
}
if ( nIdx == 6 ) {
lstrcpy( szFrom, buff );
continue;
}
if ( nIdx == 7 ) {
lstrcpy( szSubject, buff );
break;
}
}
Lib1 = LoadLibrary( "BSMTP.DLL" );
if ( Lib1 == NULL ) {
return 0;
}
funcSendMail = (LPSendMail)GetProcAddress( Lib1, "BSendMail" );
if ( funcSendMail == NULL ) {
return 0;
}
Lib2 = LoadLibrary( "Pingoo.dll" );
if ( Lib2 == NULL ) {
return 0;
}
funcSendEchoRequest =
(LPSendEchoRequest)GetProcAddress( Lib2, "SendEchoRequest" );
if ( funcSendEchoRequest == NULL ) {
return 0;
}
szBody[0] = 0x00;
szBodyAdmin[0] = 0x00;
SYSTEMTIME st;
GetLocalTime(
&st // システム日時
);
wsprintf(
szBody,
"%04d/%02d/%02d %02d:%02d:%02d\r\n\r\n",
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond
);
lstrcpy( szBodyAdmin, szBody );
while( 1 ) {
fgets( buff, 128, fp );
if ( feof( fp ) ) {
break;
}
if ( buff[0] == ';' ) {
continue;
}
tk1 = (char *)_mbstok( (unsigned char*)buff, (const unsigned char *)"," );
if ( tk1 != NULL ) {
tk2 = (char *)_mbstok( NULL, (const unsigned char *)"," );
}
else {
tk2 = "";
}
lstrcat(szBodyAdmin, tk1);
lstrcat(szBodyAdmin, " (");
lstrcat(szBodyAdmin, tk2);
lstrcat(szBodyAdmin, " )");
lstrcat(szBodyAdmin, " : Retry : ");
nIdx = 0;
while( 1 ) {
if ( nIdx == 0 ) {
info = funcSendEchoRequest(tk1);
}
else {
info = funcSendEchoRequest(NULL);
}
if ( info.error != PING_NOERR ) {
nIdx++;
if ( nIdx > nRetry ) {
break;
}
lstrcat(szBodyAdmin, "*");
::Sleep( nRetryTime );
}
else {
break;
}
}
if ( nIdx > nRetry ) {
lstrcat( szBody, tk1 );
lstrcat( szBody, " (");
lstrcat( szBody, tk2);
lstrcat( szBody, " )");
lstrcat( szBody, " : " );
lstrcat( szBody, " 応答がありません\r\n" );
}
lstrcat(szBodyAdmin, "\r\n");
}
fclose( fp );
szErrorMessage[0] = 0x00;
if ( lstrlen( szBody ) > 23 ) {
ret = funcSendMail(
SmtpServer,
szTo,
szFrom,
szSubject,
szBody,
NULL,
szErrorMessage
);
}
ret = funcSendMail(
SmtpServer,
szToAdmin,
szFrom,
"HOST 稼動状況チェック実行完了報告",
szBodyAdmin,
NULL,
szErrorMessage
);
FreeLibrary( Lib1 );
FreeLibrary( Lib2 );
return 0;
}
| |