Skip to content

Instantly share code, notes, and snippets.

@skunkie
Created July 3, 2018 07:31
Show Gist options
  • Save skunkie/2ef1caa1d0d8d5e09d767aab8587c0e5 to your computer and use it in GitHub Desktop.
Save skunkie/2ef1caa1d0d8d5e09d767aab8587c0e5 to your computer and use it in GitHub Desktop.
InnoSetup config file for Check_MK Agent
#define VERSIONFULL '1.4.0p26';
#define VERSIONSHORT '1.4.0.26';
#define WAITTIME 300;
;check the key 'only_from' in the section [global] of the file 'check_mk.ini':
#define only_from '127.0.0.1 10.0.0.8';
[Setup]
AppName=Check_MK Agent
AppVerName=Check_MK Agent v{#VERSIONFULL}
CloseApplications=no
Compression=lzma
DefaultDirName={sd}\Monitoring
DirExistsWarning=no
DisableDirPage=yes
DisableProgramGroupPage=yes
DisableReadyPage=yes
OutputBaseFilename=check_mk_installer_v{#VERSIONFULL}
OutputDir=..
SetupIconFile=..\installer.ico
SourceDir=files
Uninstallable=no
VersionInfoCompany=http://mathias-kettner.de
VersionInfoCopyright=http://mathias-kettner.de
VersionInfoDescription=http://mathias-kettner.de
VersionInfoTextVersion={#VERSIONFULL}
VersionInfoVersion={#VERSIONSHORT}
WizardImageFile=compiler:WizModernImage-IS.bmp
WizardSmallImageFile=compiler:WizModernSmallImage-IS.bmp
[Dirs]
Name: "{app}\plugins"
[Files]
Source: "check_mk.ini"; DestDir: "{app}"; Check: ReplaceIniFile()
Source: "check_mk_agent-64.exe"; DestDir: "{app}"; DestName: "check_mk_agent.exe"; Check: IsWin64; AfterInstall: InstallStartService()
Source: "check_mk_agent.exe"; DestDir: "{app}"; Check: Not IsWin64; AfterInstall: InstallStartService()
[Code]
#include 'services_unicode.iss'
const
AService = 'Check_MK_Agent'; //the name of the service 'Check_MK_Agent'
var
AFileName: String;
Counter: Integer;
IniFileName: String;
procedure InitializeWizard();
begin
AFileName := WizardDirValue + '\check_mk_agent.exe'; //the full path to the file 'check_mk_agent.exe'
IniFileName := WizardDirValue + '\check_mk.ini'; //the full path to the file 'check_mk.ini'
end;
procedure ExitProcess(exitCode:integer);
external 'ExitProcess@kernel32.dll stdcall';
procedure GetSetKey(Section, Key, Value: String);
var
GetValue: String;
begin
if IniKeyExists(Section, Key, IniFileName) then begin
GetValue := GetIniString(Section, Key, 'Value', IniFileName);
if GetValue <> Value then begin
SetIniString(Section, Key, Value, IniFileName);
end;
end
else SetIniString(Section, Key, Value, IniFileName);
end;
function CSimpleQueryService(AService: string): Longword;
//modified function SimpleQueryService()
var
ServiceStatus: _SERVICE_STATUS;
SCMHandle: Longword;
ServiceHandle: Longword;
begin
Result := 0;
try
SCMHandle := OpenSCManager('', '', SC_MANAGER_ALL_ACCESS);
if SCMHandle = 0 then
RaiseException('OpenSCManager: ' + AService + ' ' + SysErrorMessage(DLLGetLastError));
try
ServiceHandle := OpenService(SCMHandle, AService, SERVICE_ALL_ACCESS);
if ServiceHandle = 0 then
RaiseException('OpenService: ' + AService + ' ' + SysErrorMessage(DLLGetLastError));
try
ControlService(ServiceHandle, SERVICE_CONTROL_INTERROGATE, ServiceStatus);
Result := ServiceStatus.dwCurrentState;
finally
if ServiceHandle <> 0 then
CloseServiceHandle(ServiceHandle);
end;
finally
if SCMHandle <> 0 then
CloseServiceHandle(SCMHandle);
end;
except
ShowExceptionMessage;
end;
end;
procedure StopDeleteService();
var
ImagePath: String;
begin
if ServiceExists(AService) then begin
SimpleStopService(AService, True, True);
Counter := 0;
while (CSimpleQueryService(AService) <> SERVICE_STOPPED) and (Counter <= {#WAITTIME}) do begin //wait WAITTIME sec for the service to stop
Sleep(100);
Counter := Counter + 1;
end;
if CSimpleQueryService(AService) <> SERVICE_STOPPED then begin
MsgBox('Unable to stop Windows service ' + AService + ' within ' + IntToStr(Counter / 10) + ' seconds, aborting', mbInformation, MB_OK);
ExitProcess(4);
end;
if RegQueryStringValue(HKLM, 'SYSTEM\CurrentControlSet\services\' + AService, 'ImagePath', ImagePath) then begin
if ImagePath <> AFileName then begin
SimpleDeleteService(AService);
Counter := 0;
while ServiceExists(AService) and (Counter <= {#WAITTIME}) do begin //wait WAITTIME sec for the service to unregister
Sleep(100);
Counter := Counter + 1;
end;
if ServiceExists(AService) then begin
MsgBox('Unable to unregister Windows service ' + AService + ' within ' + IntToStr(Counter / 10) + ' seconds, aborting', mbInformation, MB_OK);
ExitProcess(4);
end;
end;
end;
end;
end;
function ReplaceIniFile(): Boolean;
begin
StopDeleteService();
Result := True;
if FileExists(IniFileName) then begin
GetSetKey('global', 'only_from', '{#only_from}');
GetSetKey('logwatch', 'logfile *', 'off');
Result := False;
end;
end;
procedure InstallStartService();
begin
if not ServiceExists(AService) then begin
SimpleCreateService(AService, AService, AFileName, SERVICE_AUTO_START, '', '', False, True);
Counter := 0;
while not ServiceExists(AService) and (Counter <= {#WAITTIME}) do begin //wait WAITTIME sec for the service to register
Sleep(100);
Counter := Counter + 1;
end;
if not ServiceExists(AService) then begin
MsgBox('Unable to register Windows service ' + AService + ' within ' + IntToStr(Counter / 10) + ' seconds, aborting', mbInformation, MB_OK);
ExitProcess(4);
end;
end;
SimpleStartService(AService, True, True);
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment