今天下午偶然看到我的部落格因為一個wp-cron job死了,雖然原因不明但這種死不瞑目的情況從我剛接觸IIS到現在也算是層出不窮啦...
就想說我來寫個程式監控狀況,自動重開死掉的AppPool!耶,你這該死的IIS~~
天啊,你不去讀書還在這裡幹嘛?星期二就要段考了!
我什麼都沒看到~~~~(拖走
程式主要的目的是監控,所以檢查和控制的工作外包給cUrl和AppCmd
由cUrl發送HTTP head要求,只要10秒內網站沒有回應200 OK就由AppCmd進行AppPool關閉、啟動的操作
不過AppCmd取得的網站列表實在很雜亂,所以網站名稱和URL的部分是用手打的所以很Q喔~
編譯好程式後,只要將程式和所需的外包零件放在一起,交給工作排程器去執行就OK啦~
但要記得,設定觸發條件時要把「開始位置」調成程式目錄,並賦予管理員權限喔~
--11/26更新--
加入log和timestamp,以便日後除錯用
--12/3更新--
新增檢查網路狀態,避免無謂的檢查
--1/12更新--
增加cUrl過濾條件
/* This source file is composed using MSVC11, it may or may not work under your environment. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> int main(void) { int isUp, i, p, netOK, netRetry; char logdata[500], buffer[300], sitename[20][300], siteurl[20][300]; char cUrl[100], appcmd_start[300], appcmd_stop[300], *pch, *next_pch; time_t rawtime; FILE *pPipe, *siteList, *appCMD, *logfile; /* Check network status first */ for(netRetry=0; netRetry<3; netRetry++) { if( (pPipe = _popen( "ping 8.8.8.8", "rt" )) == NULL ) { exit(10); } else { netOK = 0; /* Read pipe until end of file. */ while( !feof( pPipe ) ) { if( fgets( buffer, 300, pPipe ) != NULL ) { if(strstr(buffer, "(0% 遺失)") != NULL ) { netOK = 1; break; } } } /* Close pipe of pPipe. */ _pclose( pPipe ); if(netOK == 0 && netRetry == 2) { exit(11); } else if(netOK == 1) { break; } } } /* Read site list */ if( fopen_s( &siteList, "sitelist.txt", "r") != 0 ) { exit(1); } else { p = 0; while(!feof(siteList)) { if( fgets( buffer, 300, siteList ) != NULL ) { pch = strtok_s (buffer, ",\n", &next_pch); strcpy_s(sitename[p], pch); pch = strtok_s (NULL, ",\n", &next_pch); strcpy_s(siteurl[p], pch); p++; } } fclose(siteList); } /* Start detecting website status */ for( i=0; i<p; i++) { isUp = 0; strcpy_s(cUrl, "curl\\curl.exe -I --connect-timeout 10 "); strcat_s(cUrl, siteurl[i]); if( (pPipe = _popen( cUrl, "rt" )) == NULL ) { exit(2); } else { /* Read pipe until end of file. */ while( !feof( pPipe ) ) { if( fgets( buffer, 300, pPipe ) != NULL ) { if( strstr(buffer, "200 OK") != NULL || strstr(buffer, "301 Moved Permanently") != NULL || strstr(buffer, "302 Moved Temporarily") != NULL || strstr(buffer, "Host not found") != NULL ) { isUp = 1; break; } } } /* Close pipe of pPipe. */ _pclose( pPipe ); } if(isUp == 0) { strcpy_s(appcmd_start, "%windir%\\system32\\inetsrv\\appcmd start apppool "); strcpy_s(appcmd_stop, "%windir%\\system32\\inetsrv\\appcmd stop apppool "); strcat_s(appcmd_start, sitename[i]); strcat_s(appcmd_stop, sitename[i]); if( (appCMD = _popen( appcmd_stop, "rt" )) == NULL ) { exit(3); } else { time( &rawtime ); strcpy_s(logdata, "#~" ); strcat_s(logdata, asctime( localtime( &rawtime ) ) ); while(fgets(buffer, 300, appCMD)) { strcat_s(logdata, buffer); } _pclose( appCMD ); if( (appCMD = _popen( appcmd_start, "rt" )) == NULL ) { exit(4); } } while(fgets(buffer, 300, appCMD)) { strcat_s(logdata, buffer); } _pclose( appCMD ); strcat_s(logdata, "\n" ); if( fopen_s( &logfile, "logfile.txt", "a+") != 0 ) { exit(5); } else { fprintf(logfile, "%s", logdata); fclose( logfile ); } } } return 0; }
留言