フリー dll で ping バッチ build VC6

  使用した dll



Pingoo
Basp21



  入力テキスト



  
;SMTP サーバー
xxx.xxx.xxx.xxx
;リトライ回数
4
;リトライ間隔( ミリ秒 )
1000
;エラー送信先 ( 複数指定は間に TAB )
メールアドレス
;処理結果通知先 ( 複数指定は間に TAB )
メールアドレス
;送信元
メールアドレス
;タイトル
【重要】HOST 稼動状況の警告
;対象 HOST リスト
xxx.xxx.xxx.xxx,名称
xxx.xxx.xxx.xxx,名称
xxx.xxx.xxx.xxx,名称
xxx.xxx.xxx.xxx,名称
  



  ソース

すぐ欲しかったので、あまりエレガントでは無いです・・・
( また、内部処理なので SMTP サーバーに対する制限もありません。というか ping の応答があるという前提ですし )

  
#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;
}

  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ