Announcement

Collapse
No announcement yet.

One step install and starting of windows agent

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    Determining windows architecture

    Originally posted by Mikrodots View Post
    Still testing but so far so good...

    Code:
    echo off
    ECHO.
    
    REM Mike Doherty 
    REM Mikrodots, Inc.
    REM http://www.mikrodots.com
    
    REM Zabbix Server Address
    
    REM UNC Path to Zabbix Agent installation files
    set FilePath64="\\myserver\zabbix\win64\zabbix_agentd.exe"
    set FilePath32="\\myserver\zabbix\win32\zabbix_agentd.exe" 
    
    REM Local Variables
    set service="ZABBIX Agent"
    set SUCCESS="Successfully"
    REM I haven't tested this on a 64 bit OS yet
    set qry="HKLM\System\CurrentControlSet\Services\Zabbix Agent"
    
    ECHO ---------- Start Zabbix Agent Script...
    ECHO.
    
    :IsServiceInstalled
    ECHO.
    ECHO Is %service% Installed?
    reg query %qry% | find /I %service% >NUL
    ECHO  If Zero the service is Installed:%ERRORLEVEL%
     If NOT ERRORLEVEL 1 (
      ECHO   YES, %service% Is Installed
      GOTO IsServiceRunning
      ) ELSE (
    	 ECHO NO, %service% Is NOT Installed
    	 GOTO InstallService
    	 )
    GOTO END
    
    :IsServiceRunning
    ECHO.
    ECHO Is %service% Running?
    net start | find /I %service% >NUL 
    ECHO  If Zero %service% is Running:%ERRORLEVEL% 
     If NOT ERRORLEVEL 1 (
       ECHO   YES, %service% IS Running
       ) ELSE (
       	  ECHO ----------- NO, %service% IS NOT Running -----------------
       	  GOTO StartService
    	  )
    GOTO END
    
    :StartService
    ECHO.
    ECHO Try to Start %service%
    net start %service% | FIND /I %SUCCESS% >NUL
    ECHO  If Zero the %service% started %ERRORLEVEL%
     If NOT ERRORLEVEL 1 ( 
       echo   YES %service% Started Successfully
       ) ELSE ( 
    	  ECHO ------------!!!!! %service% DID NOT START !!!!!--------------
    	  )
    GOTO END
    
    :GetInstallFilePath
    ECHO.
    REM 32Bit or 64 Bit?
    REM Could use a better test, this will fail without a C: drive
    if exist "C:\Program Files (x86)" (
      set FilePath=%FilePath64%
      ) else (
    	  set FilePath=%FilePath32%
    	  )
    ECHO Path to zabbix_agentd.exe:  %FilePath%
    
    REM Verify that the installation file exists
    XCOPY  %FilePath% /L | find /I %FilePath%  >NUL 
    ECHO  If Zero %FilePath% Exists: %ERRORLEVEL% 
     If NOT ERRORLEVEL 1 (
       ECHO   YES, %FilePath% Exists
       GOTO END  REM InstallService
       ) ELSE (
       	  ECHO %FilePath% 
    	  ECHO ------------IS NOT VALID -----------------
    	  ECHO -------------- EXITING -------------------
       	  GOTO END
    	  )
    GOTO END
    
    :InstallService
    ECHO.
    REM Change working directory
    CD %SystemDrive%\
    REM Make sure we're on the right partition
    %SystemDrive%
    
    ECHO Copying %FilePath% To %SystemDrive%
    xcopy /F /Y /R %FilePath%
    
    ECHO Writing zabbix_agentd.conf for %COMPUTERNAME%.%USERDNSDOMAIN%
    ECHO Server=1.2.3.4 > zabbix_agentd.conf
    ECHO ServerPort=10050 >> zabbix_agentd.conf
    ECHO Hostname=%COMPUTERNAME%.%USERDNSDOMAIN% >> zabbix_agentd.conf
    ECHO ListenPort=10050 >> zabbix_agentd.conf
    ECHO StartAgents=5 >> zabbix_agentd.conf
    ECHO DebugLevel=3 >> zabbix_agentd.conf
    ECHO LogFile= %SystemDrive%\zabbix_agentd.log >> zabbix_agentd.conf
    ECHO Timeout=5 >> zabbix_agentd.conf
    
    ECHO Installing %service% as a service
    %SystemDrive%\zabbix_agentd.exe -i -c %SystemDrive%\zabbix_agentd.conf
    ECHO If Zero the %service% Installed Successfully: %ERRORLEVEL%
     If ERRORLEVEL == 0 ( 
       ECHO   YES, %service% Installed Successfully
       GOTO IsServiceRunning
       ) ELSE ( 
    	  ECHO ------------!!!!! %service% Installation FAILED !!!!!--------------
    	  GOTO END
    	  )
    ECHO Finished Installing Zabbix Agent!
    
    GOTO END
    
    :END
    ECHO.
    ECHO The Script has finnished running!
    Only tested this on a 32 bit Vista system so far...

    I'd like to get some feedback from anyone who tests this.

    Thanks,

    Mikrodots
    There is an elegant way of finding the OS architecture (32 or 64 bit) by using

    wmic OS get OSArchitecture | find /I "64-bit"
    if not errorlevel 1 (set FilePath=%FilePath64%
    ) else (
    set FilePath=%FilePath32%
    )
    ECHO Path to zabbix_agentd.exe: %FilePath%

    Comment


      #17
      ZABBIX (RE)Install Script

      Hi All

      I've made my own version of the install script originally posted here.
      This script stops the service, uninstalls the agent and then installs and starts the new version of the agent from the server. It also adds a firewall rule (windows 7 / server 2008) and has a little more sophisticated way of telling the architecture.

      An install directory is also made an option and it supports installs with proxy as well.

      Let me hear what you think.

      /Emil



      @echo off
      ECHO.


      REM ***** Parameters START *****

      REM UNC Path to Zabbix Agent installation files
      set FilePath64=\\myserver\zabbixinstall\win64\zabbix_a gentd.exe
      set FilePath32=\\myserver\zabbixinstall\win32\zabbix_a gentd.exe


      REM Agent configuration
      REM ----- IMPORTANT ----- Set proxyip to NONE if no proxy is used
      set proxyip=0.0.0.0
      set serverip=0.0.0.0
      set listenport=10050
      set serverport=10051
      set InstallDir=ZabbixAgent

      REM Local Variables
      set service="Zabbix Agent"
      set SUCCESS="Successfully"
      set qry="HKLM\System\CurrentControlSet\Services\Zabbix Agent"

      REM ***** Parameters END *****

      ECHO ---------- Start Zabbix Agent (Re)Install Script...
      ECHO.

      :IsServiceInstalled
      ECHO.
      ECHO Is %service% Installed?
      reg query %qry% | find /I %service% >NUL
      If NOT ERRORLEVEL 1 (
      ECHO %service% Is Installed
      GOTO IsServiceRunning
      ) ELSE (
      ECHO %service% Is NOT Installed
      GOTO GetInstallFilePath
      )
      GOTO END

      :IsServiceRunning
      ECHO.
      ECHO Is %service% Running?
      net start | find /I %service% >NUL
      If NOT ERRORLEVEL 1 (
      ECHO %service% IS Running
      GOTO StopService
      ) ELSE (
      ECHO %service% is not running
      GOTO RemoveService
      )
      GOTO END

      :StopService
      ECHO.
      ECHO Try to Stop %service%
      net stop %service% >NUL
      If NOT ERRORLEVEL 1 (
      echo %service% Stopped Successfully
      GOTO RemoveService
      ) ELSE (
      ECHO ------------!!!!! %service% DID NOT STOP !!!!!--------------
      )
      GOTO END

      :RemoveService
      ECHO.
      REM Change working directory and partition and create InstallDir if it does not exist
      if not exist %SystemDrive%\%InstallDir%\. mkdir %SystemDrive%\%InstallDir%
      CD %SystemDrive%\%InstallDir%
      %SystemDrive%

      REM Remove Installation
      ECHO Remove %service% installation
      echo Server=%server% >zabbix_agentd.conf
      ECHO Copying %FilePath% To %SystemDrive%\%InstallDir%
      xcopy /F /Y /R %FilePath%
      %SystemDrive%\%InstallDir%\zabbix_agentd.exe --uninstall -c %SystemDrive%\%InstallDir%\zabbix_agentd.conf
      If ERRORLEVEL == 0 (
      ECHO %service% Uninstalled Successfully!
      GOTO GetInstallFilePath )
      ELSE
      (
      ECHO %Service Uninstall NOT successful!
      GOTO END
      )
      GOTO END

      :GetInstallFilePath
      ECHO.
      REM 32Bit or 64 Bit?

      wmic OS get OSArchitecture | find /I "64-bit"
      if not errorlevel 1 (set FilePath=%FilePath64%
      ECHO 64 Bit Operating System - using %FilePath%
      ) else (
      set FilePath=%FilePath32%
      ECHO 32 Bit Operating System - using %FilePath%
      )

      REM Verify that the installation file exists
      XCOPY %FilePath% /L /Y | find /I "%FilePath%" >NUL
      If NOT ERRORLEVEL 1 (
      GOTO InstallService
      ) ELSE (
      ECHO %FilePath%
      ECHO ------------IS NOT VALID -----------------
      ECHO -------------- EXITING -------------------
      GOTO END
      )
      GOTO END

      :InstallService
      ECHO.
      REM Change working directory and partition and create InstallDir if it does not exist
      if not exist %SystemDrive%\%InstallDir%\. mkdir %SystemDrive%\%InstallDir%
      CD %SystemDrive%\%InstallDir%
      %SystemDrive%

      if exist %systemdrive%\%InstallDir%\zabbix_agentd.exe del /Q %systemdrive%\%InstallDir%\zabbix_agent.exe
      ECHO Copying %FilePath% To %SystemDrive%\%InstallDir%
      xcopy /F /Y /R %FilePath%


      ECHO Writing Configuration File for %COMPUTERNAME%.%USERDNSDOMAIN%
      IF NOT %proxyip%=="NONE" (ECHO Server=%proxyip%,%serverip% > zabbix_agentd.conf
      ) ELSE (
      ECHO Server=%serverip% >zabbix_agentd.conf
      )
      ECHO ServerPort=%serverport% >> zabbix_agentd.conf
      ECHO Hostname=%COMPUTERNAME%at%USERDNSDOMAIN% >> zabbix_agentd.conf
      ECHO ListenPort=%listenport% >> zabbix_agentd.conf
      ECHO StartAgents=5 >> zabbix_agentd.conf
      ECHO DebugLevel=3 >> zabbix_agentd.conf
      ECHO LogFile= %SystemDrive%\%InstallDir%\zabbix_agentd.log >> zabbix_agentd.conf
      ECHO Timeout=5 >> zabbix_agentd.conf

      ECHO Installing %service% as a service
      %SystemDrive%\%InstallDir%\zabbix_agentd.exe -i -c %SystemDrive%\%InstallDir%\zabbix_agentd.conf
      If ERRORLEVEL == 0 (
      ECHO %service% Installed Successfully
      GOTO StartService
      ) ELSE (
      ECHO ----- %service% Installation FAILED -----
      GOTO END
      )

      GOTO END

      :StartService
      ECHO.
      ECHO Adding rule to firewall...
      netsh advfirewall firewall add rule name=%service% protocol=TCP dir=in localport=%listenport% action=allow
      echo.
      ECHO Try to Start %service%
      net start %service% >NUL
      If NOT ERRORLEVEL 1 (
      echo %service% Started Successfully
      ) ELSE (
      ECHO ------------!!!!! %service% DID NOT START !!!!!--------------
      )
      GOTO END

      :END
      ECHO.
      ECHO The Script has finished running!

      Comment


        #18
        My Version.

        If have build also my own script:


        1) First i detect if its a reinstall of a new install.
        2) Then I check if its a 32 of 64 bit machine.
        3) Then we ask how the machine is called in Zabbix
        4) Then we ask which proxy is going to monitor the machine.
        5) Then we check if the zabbix server is able to connect to the agent. (agent.php on the zabbix server)

        Zabbix.cmd:
        Code:
        @ECHO OFF
        
        CLS
        
        
        NET START | FIND "Zabbix Agent">NUL
        IF "%ERRORLEVEL%"=="1" GOTO Detect
        IF "%ERRORLEVEL%"=="0" GOTO Reinstall
        
        :Reinstall
         NET STOP "Zabbix Agent"
         SC DELETE "Zabbix Agent"
         IF EXIST C:\Zabbix\ (
          RMDIR /S /Q C:\Zabbix\
          CLS
          GOTO :Detect
         )
        
        :Detect
        IF "%PROCESSOR_ARCHITECTURE%" == "x86" (
         GOTO x86
        )
        IF "%PROCESSOR_ARCHITECTURE%" == "i86" (
         GOTO x86
        )
        IF "%PROCESSOR_ARCHITECTURE%" == "x64" (
         GOTO x64
        )
        IF "%PROCESSOR_ARCHITECTURE%" == "AMD64" (
         GOTO x64
        )
        IF "%PROCESSOR_ARCHITECTURE%" == "amd64" (
         GOTO x64
        )
        
        :x86
         
          set /p answer=What is the hostname in Zabbix
         ECHO Which proxy is monitoring the server ? 
         ECHO .
         ECHO .
         set /p answer2=(proxy ip):
         MKDIR C:\Zabbix\
         ECHO Server=%answer2%, server > C:\Zabbix\zabbix_agentd.conf
         ECHO EnableRemoteCommands=1 >> C:\Zabbix\zabbix_agentd.conf
         ECHO Hostname=%answer% >> C:\Zabbix\zabbix_agentd.conf
         ECHO StartAgents=32 >> C:\Zabbix\zabbix_agentd.conf
         COPY \\10.208.60.61\install\x86\*.* C:\Zabbix\
         GOTO Install
        
        :x64
         set /p answer=What is the hostname in Zabbix
         ECHO Which proxy is monitoring the server ? 
         ECHO .
         ECHO .
         set /p answer2=(proxy ip):
         MKDIR C:\Zabbix\
         ECHO Server=%answer2%, server > C:\Zabbix\zabbix_agentd.conf
         ECHO EnableRemoteCommands=1 >> C:\Zabbix\zabbix_agentd.conf
         ECHO Hostname=%answer% >> C:\Zabbix\zabbix_agentd.conf
         ECHO StartAgents=32 >> C:\Zabbix\zabbix_agentd.conf
         COPY \\10.208.60.61\install\x64\*.* C:\Zabbix\
         GOTO Install
        
        :Install
         CLS
         C:\Zabbix\zabbix_agentd.exe -i --config C:\Zabbix\zabbix_agentd.conf
         C:\Zabbix\zabbix_agentd.exe --config C:\Zabbix\zabbix_agentd.conf -s
         GOTO Check
        
        :Check
         ECHO START iexplore http://<server ip>/screen/agent.php
         GOTO End
        
        :End
        screen/agent.php
        PHP Code:
        <?PHP
        error_reporting
        (0);

        if((isset(
        $_GET['ip'])) && (!empty($_GET['ip'])))
        {
         
        $to fopen("hosts.log""a");
         if(
        fsockopen($_GET['ip'], 10050))
         {
          
        fwrite($to"\nOK: ".$_GET['ip']);
          
        $style "border : 1px solid #39C300; color : #39C300; padding-left : 25px; background-image:url(http://10.208.60.61/screen/images/ok.png); background-repeat : no-repeat;";
          PRINT 
        '<div style="text-align : center; color : #39C300;">De agent is succesvol ge├»nstalleerd!</div>';
         } else
         {
          
        fwrite($to"\nERROR: ".$_GET['ip']);
          
        $style "border : 1px solid #FF0000; color : #FF0000;";
          PRINT 
        '<div style="text-align : center; color : #FF0000;">Controleer of de Zabbix service is gestart en/of TCP poort 10050 niet geblokkeerd wordt door een firewall.</div>';
         }
         
        fclose($to);
        } else
        {
         
        $style "border : 1px solid #000;";
        }
        ?>
        <form method="get" action="" style="text-align : center;">
         <input type="text" name="ip" style="width : 150px; <?PHP PRINT $style?>" value="<?PHP if((isset($_GET['ip'])) && (!empty($_GET['ip']))){ PRINT $_GET['ip']; } else { PRINT $_SERVER['REMOTE_ADDR']; } ?>" /> <input type="submit" value="Controleer" />
        </form>
        Last edited by Voxie; 02-12-2011, 10:36. Reason: typo

        Comment


          #19
          Nice

          Nice job.

          Good inspiration to improve my own script

          Comment


            #20
            Thanks for the script. I just wanted to post the changes I made. This also helps with finding the network interface name and adds it to the config as NetIn and NetOut.

            The one that comes with Zabbix is a little outdated but a good starting point?

            Code:
            @ECHO OFF
            
            REM Config Settings
            SET my_zabbix_share=\\ServerNameOrIP\Share\zabbix_clients\bin\
            SET my_zabbix_install_path=C:\Zabbix\
            SET my_zabbix_config=C:\Zabbix\zabbix_agentd.conf
            SET temp_notepad_file=c:\findinterface.txt
            
            CLS
            
            
            NET START | FIND "Zabbix Agent">NUL
            IF "%ERRORLEVEL%"=="1" GOTO Pre-Install
            IF "%ERRORLEVEL%"=="0" GOTO Reinstall
            
            :Reinstall
             NET STOP "Zabbix Agent"
             SC DELETE "Zabbix Agent"
             IF EXIST %my_zabbix_install_path% (
              RMDIR /S /Q %my_zabbix_install_path%
              CLS
              GOTO :Pre-Install
             )
            
            :Pre-Install
            
             REM Open notepad with possible config values
             ECHO "View the Text File that opens for Config Options."
             ECHO Hostname is? > %temp_notepad_file%
             hostname >> %temp_notepad_file%
             ECHO . >> %temp_notepad_file%
             ECHO This could be your Network Interface(s)? >> %temp_notepad_file%
             typeperf -qx | find "Network Interface" >> %temp_notepad_file%
             start notepad.exe %temp_notepad_file%
             ECHO .
            
             REM Get the values from the user
             set /p answer=What is the hostname in Zabbix: 
             ECHO .
             set /p answer2=Server/Proxy IP: 
             Echo .
             set /p nicname=Host Network Card Name:
            
             REM Write the values to the config file
             MKDIR %my_zabbix_install_path%
             ECHO LogFile=%my_zabbix_install_path%zabbix_agentd.log > %my_zabbix_config%
             ECHO Server=%answer2% >> %my_zabbix_config%
             ECHO EnableRemoteCommands=1 >> %my_zabbix_config%
             ECHO Hostname=%answer% >> %my_zabbix_config%
             ECHO PerfCounter = NetIn,"\Network Interface(%nicname%)\Bytes Received/sec",60 >> %my_zabbix_config%
             ECHO PerfCounter = NetOut,"\Network Interface(%nicname%)\Bytes Sent/sec",60 >> %my_zabbix_config%
            
             REM Cleanup the notepad file. User doesn't have to close the file.
             set /p nothing=Close Notepad and Press Enter:
             DEL %temp_notepad_file%
            
             GOTO :Detect
            
            
            :Detect
            IF "%PROCESSOR_ARCHITECTURE%" == "x86" (
             GOTO x86
            )
            IF "%PROCESSOR_ARCHITECTURE%" == "i86" (
             GOTO x86
            )
            IF "%PROCESSOR_ARCHITECTURE%" == "x64" (
             GOTO x64
            )
            IF "%PROCESSOR_ARCHITECTURE%" == "AMD64" (
             GOTO x64
            )
            IF "%PROCESSOR_ARCHITECTURE%" == "amd64" (
             GOTO x64
            )
            
            :x86
             COPY %my_zabbix_share%win32\*.* %my_zabbix_install_path%
             GOTO Install
            
            :x64
             COPY %my_zabbix_share%win64\*.* %my_zabbix_install_path%
             GOTO Install
            
            :Install
             CLS
             %my_zabbix_install_path%zabbix_agentd.exe -i --config %my_zabbix_config%
             %my_zabbix_install_path%zabbix_agentd.exe --config %my_zabbix_config% -s
            
            :End

            Comment

            Working...
            X