Mar
02
|
Après l’avoir laisser tourner un peu plus de 24 heures, j’ai mis à jour le code du service pour contrôler ma connexion internet…
Cependant, voici le résultat rapidement obtenu avec les données récoltées :
Si je n’avais pas de problème de connexion, nous verrions un rectangle plein… les zones blanches correspondent à des pertes de synchronisation, et donc des coupures web…
Dans cette nouvelle version, j’ai modifié les choses suivantes :
- Téléchargement d’un bout de page HTML… en effet, le ping en suffisait pas à faire tomber la connexion !
- Stockage de la première et dernière valeur.. Excel a du mal avec trop de valeurs…
- Ajout également de la date au format Unix… Toujours pour Excel, il me propose une plage minimale d’une journée.. donc, j’ai mis un numérique
- Calcul systématique du nom de fichier pour changer de fichier de log à chaque nouvelle journée
Voici le code actualisé :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | unit main; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs, ExtCtrls, IdBaseComponent, IdComponent, IdRawBase, IdRawClient, IdIcmpClient, StrUtils, DateUtils, WinInet; const LONG_INTERVAL = 180000; // toutes les 3mn SHORT_INTERVAL = 15000; // toutes les 15s SHORT_RETRY = 50; // 50 essaies rapides après un plantage SUCCESS = 1; FAILURE = 0; type TConnectionLoggerService = class(TService) tmrCheck: TTimer; idcmpclntPinger: TIdIcmpClient; procedure ServiceExecute(Sender: TService); procedure tmrCheckTimer(Sender: TObject); procedure ServicePause(Sender: TService; var Paused: Boolean); procedure ServiceContinue(Sender: TService; var Continued: Boolean); private { Déclarations privées } iLastValue: Integer; dtLastOtherValue: TDateTime; iSpeedRetry: Integer; fPointer: TextFile; procedure WriteIt(sToWrite: string); function Ping(): SmallInt; function AddLeadingZeroes(const iNumber: Integer; iLength: Integer = 2): string; function CheckIt: SmallInt; function FakeDownload: SmallInt; public function GetServiceController: TServiceController; override; { Déclarations publiques } end; var ConnectionLoggerService: TConnectionLoggerService; implementation {$R *.DFM} procedure ServiceController(CtrlCode: DWord); stdcall; begin ConnectionLoggerService.Controller(CtrlCode); end; function TConnectionLoggerService.AddLeadingZeroes(const iNumber: Integer; iLength: Integer): string; begin Result := SysUtils.Format('%.*d', [iLength, iNumber]) ; end; function TConnectionLoggerService.CheckIt: SmallInt; begin // Téléchargement d'un bout de page ET ping du serveur Result := (FakeDownload + Ping); if (Result = 2) then begin Dec(iSpeedRetry); if (iSpeedRetry = 0) then begin tmrCheck.Interval := LONG_INTERVAL; end; end else begin iSpeedRetry := SHORT_RETRY; tmrCheck.Interval := SHORT_INTERVAL; end; // On n'écrit dans les logs que si le résultat change if (iLastValue <> Result) then begin // On ne stocke pas en double la première valeur if (iLastValue <> -1) then begin // Dernière précédente valeur WriteIt(DateTimeToStr(dtLastOtherValue) + ';' + IntToStr(DateTimeToUnix(dtLastOtherValue)) + ';' + IfThen(iLastValue = 2, '1', '0')); end; // Caption Time Unix Format 1=Ok ; 0=tombé WriteIt(DateTimeToStr(Now) + ';' + IntToStr(DateTimeToUnix(Now)) + ';' + IfThen(Result = 2, '1', '0')); iLastValue := Result; end; dtLastOtherValue := Now; end; function TConnectionLoggerService.FakeDownload: SmallInt; var hSession, hURL: HInternet; Buffer: array[1..1024] of Byte; BufferLen: DWORD; begin Result := FAILURE; hSession := InternetOpen(PChar(ExtractFileName(ParamStr(0))), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ; try hURL := InternetOpenURL(hSession, PChar('https://portail.free.fr/'), nil, 0, 0, 0) ; try InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) ; Result := SUCCESS; finally InternetCloseHandle(hURL) end finally InternetCloseHandle(hSession) end end; function TConnectionLoggerService.GetServiceController: TServiceController; begin Result := ServiceController; end; function TConnectionLoggerService.Ping(): SmallInt; begin Result := SUCCESS; with idcmpclntPinger do begin try Ping(); except Result := FAILURE; Exit; end; if (ReplyStatus.ReplyStatusType <> rsEcho) then Result := FAILURE; //pas d'écho, on renvoi false. end; end; procedure TConnectionLoggerService.ServiceContinue(Sender: TService; var Continued: Boolean); begin CheckIt; tmrCheck.Enabled := True; // WriteIt('Restarted @ ' + DateTimeToStr(Now)); Continued := True; end; procedure TConnectionLoggerService.ServiceExecute(Sender: TService); begin iLastValue := -1; WriteIt('Started @ ' + DateTimeToStr(Now)); tmrCheck.Interval := LONG_INTERVAL; // On vérifie au démarrage CheckIt; // On revérifiera à intervalles réguliers tmrCheck.Enabled := True; while not Terminated do ServiceThread.ProcessRequests(True);// wait for termination WriteIt('Stopped @ ' + DateTimeToStr(Now)); tmrCheck.Enabled := False; end; procedure TConnectionLoggerService.ServicePause(Sender: TService; var Paused: Boolean); begin // Une dernière vérification avant l'arrêt... CheckIt; tmrCheck.Enabled := False; // WriteIt('Paused @ ' + DateTimeToStr(Now)); Paused := True; end; procedure TConnectionLoggerService.tmrCheckTimer(Sender: TObject); begin CheckIt; end; procedure TConnectionLoggerService.WriteIt(sToWrite: string); var sFileName: string; begin // Je recalcule à chaque fois... parce que je ne suis pas radin en CPU.. et pour que le changement de jour fonctionne tout seul... sFileName := ExtractFilePath(ParamStr(0)) + 'ConnectionLogger-' + IntToStr(YearOf(Now)) + '-' + AddLeadingZeroes(MonthOf(Now)) + '-' + AddLeadingZeroes(DayOf(Now)) + '.log'; AssignFile(fPointer, sFileName); if FileExists(sFileName) then Append(fPointer) else Rewrite(fPointer); Writeln(fPointer, sToWrite); CloseFile(fPointer); end; end. |
Si vous avez des idées qui pourraient améliorer mon service, n’hésitez pas… (Tout n’est pas optimisé, je le sais.. le but était d’avoir rapidement à disposition des données me permettant d’investiguer…)
Derniers commentaires