Ad Widget

Collapse

Running a bash script on 50+ servers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yatta
    Member
    • Feb 2022
    • 82

    #1

    Running a bash script on 50+ servers

    Hi,
    I have a script file on my Zabbix server (/usr/lib/zabbix/alertscripts/script.sh) and I want to run this script on all servers.

    I can do that with SSH but I want Zabbix agents should run this script on all servers.

    I guess with "User parameters" I can do that but it's not clear for me how to do that. Its says we need to add the command "zabbix_agentd.conf" on the document, but it seems not logical to add all bash commands to the "zabbix:agent.conf" file.

    Can you please show me how to achieve that step by step?

    Regards.
  • tim.mooney
    Senior Member
    • Dec 2012
    • 1427

    #2
    Originally posted by yatta
    Hi,
    I have a script file on my Zabbix server (/usr/lib/zabbix/alertscripts/script.sh) and I want to run this script on all servers.

    I guess with "User parameters" I can do that but it's not clear for me how to do that. Its says we need to add the command "zabbix_agentd.conf" on the document, but it seems not logical to add all bash commands to the "zabbix:agent.conf" file.
    You wouldn't add "all bash commands", you would just add the path to your script.sh. If your script requires command line parameters to function correctly, you must create your UserParameter so that your custom item key takes parameters.

    https://www.zabbix.com/documentation...userparameters

    Example:

    Create the file /etc/zabbix/zabbix_agentd.d/mysite-myscript.conf with a text editor and within that file have the contents:

    Code:
    # define a custom item key that this agent knows, called my_site_my_script, that will run /usr/lib/zabbix/zlertscripts/script.sh
    UserParameter=my_site_my_script,/usr/lib/zabbix/alertscripts/script.sh
    After you've created that text file with those contents, make sure the agent is restarted.

    Then you can use "my_site_my_script" as an item key when creating an item for the host(s) where the custom user parameter is defined.

    I recommend you name all your custom items with a prefix that identifies them as something unique to your workplace. For example, if you work at "Amazon", then use "amazon" instead of "my_site" in the item key and instead of "mysite" in the first part of the file name. This will help people that are less experienced than you (your coworkers, or someone else that has to help with Zabbix administration if you're unavailable) with Zabbix.

    Comment

    • Hamardaban
      Senior Member
      Zabbix Certified SpecialistZabbix Certified Professional
      • May 2019
      • 2713

      #3
      For run command by agent you can use “system.run[command,<mode>]“ key.

      Comment


      • yatta
        yatta commented
        Editing a comment
        On the document page says "Note: system.run items are disabled by default."
        -Should I enabled system.run on only Zabbix server or it should be enabled on all server?

        -So I have a bash script file on "/usr/lib/zabbix/alertscripts/hostname.sh", how can run this on all servers? Is this how it should be? --> system.run[sh /usr/lib/zabbix/alertscripts/hostname.sh]
    • yatta
      Member
      • Feb 2022
      • 82

      #4
      Originally posted by tim.mooney

      You wouldn't add "all bash commands", you would just add the path to your script.sh. If your script requires command line parameters to function correctly, you must create your UserParameter so that your custom item key takes parameters.

      https://www.zabbix.com/documentation...userparameters

      Example:

      Create the file /etc/zabbix/zabbix_agentd.d/mysite-myscript.conf with a text editor and within that file have the contents:

      Code:
      # define a custom item key that this agent knows, called my_site_my_script, that will run /usr/lib/zabbix/zlertscripts/script.sh
      UserParameter=my_site_my_script,/usr/lib/zabbix/alertscripts/script.sh
      After you've created that text file with those contents, make sure the agent is restarted.

      Then you can use "my_site_my_script" as an item key when creating an item for the host(s) where the custom user parameter is defined.

      I recommend you name all your custom items with a prefix that identifies them as something unique to your workplace. For example, if you work at "Amazon", then use "amazon" instead of "my_site" in the item key and instead of "mysite" in the first part of the file name. This will help people that are less experienced than you (your coworkers, or someone else that has to help with Zabbix administration if you're unavailable) with Zabbix.

      Hi again! I created "/etc/zabbix/zabbix_agentd.d/hostnametest.conf" file and only Zabbix server(not on all servers, and I guess that's how it should be?), restarted agent on Zabbix server

      After that I'm trying to create an Item but not sure how to do. What should I choose?

      Click image for larger version

Name:	Capture.jpg
Views:	4027
Size:	55.6 KB
ID:	444159

      Thanks!

      Comment

      • cyber
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Dec 2006
        • 4807

        #5
        If you use "zabbix agent userparameters"... then it should be quite obvious, what the item type would be...
        Can be also " ... (active)"

        If you want to run it in all your servers, you need that config in all servers and also that script in all servers...

        Comment

        • Hamardaban
          Senior Member
          Zabbix Certified SpecialistZabbix Certified Professional
          • May 2019
          • 2713

          #6
          For system.run you need enabled this option at AGENTs config -> EnableRemoteCommands
          https://www.zabbix.com/documentation.../zabbix_agentd
          and https://www.zabbix.com/documentation...estrict_checks !!

          Comment

          • yatta
            Member
            • Feb 2022
            • 82

            #7
            Originally posted by cyber
            If you use "zabbix agent userparameters"... then it should be quite obvious, what the item type would be...
            Can be also " ... (active)"

            If you want to run it in all your servers, you need that config in all servers and also that script in all servers...
            But that's the problem here and our goal, I want to run this script on all servers, not only on Zabbix server.
            Copying that script and config to all servers is not an easy task.

            Isn't there another way possible? I guess SSH will be left the only choice here.

            Comment

            • yatta
              Member
              • Feb 2022
              • 82

              #8
              Originally posted by Hamardaban
              For system.run you need enabled this option at AGENTs config -> EnableRemoteCommands
              https://www.zabbix.com/documentation.../zabbix_agentd
              and https://www.zabbix.com/documentation...estrict_checks !!
              There are tens of servers, I can't update every server agent's config. Not an easy task.
              I guess running script with SSH will be the best option.
              Last edited by yatta; 05-05-2022, 10:39.

              Comment

              • cyber
                Senior Member
                Zabbix Certified SpecialistZabbix Certified Professional
                • Dec 2006
                • 4807

                #9
                Originally posted by yatta
                But that's the problem here and our goal, I want to run this script on all servers, not only on Zabbix server.
                There is no problem with the goal...
                Here's your goal...

                Originally posted by yatta
                Hi,
                I have a script file on my Zabbix server (/usr/lib/zabbix/alertscripts/script.sh) and I want to run this script on all servers.

                I can do that with SSH but I want Zabbix agents should run this script on all servers.

                So... people are telling you, how to make your agents run it... Sorry, that it does not suit you, even if you stated it in your goal...

                Comment

                • yatta
                  Member
                  • Feb 2022
                  • 82

                  #10
                  Yes, the goal is running on all servers with Zabbix agent, but it seems updating agent and conf files on all servers will be painfull. Better to continue with SSH. Thanks for your help!

                  Comment

                  • tim.mooney
                    Senior Member
                    • Dec 2012
                    • 1427

                    #11
                    Originally posted by yatta
                    Yes, the goal is running on all servers with Zabbix agent, but it seems updating agent and conf files on all servers will be painfull. Better to continue with SSH. Thanks for your help!
                    You would be copying 2 files to "tens of servers" and then running a command on each system to restart the agent. If that's painful or a barrier to setup, then I urge you to look into tools that will help you scale your ability to manage systems. Repeating a set of actions for every system in your environment is a common system management task. This particular set of repeated actions is simple enough that you don't need a configuration management system like puppet, ansible, chef, etc. to accomplish it, but those are probably worth a look anyway, for the more complicated management tasks.

                    Good luck!

                    Comment


                    • yatta
                      yatta commented
                      Editing a comment
                      Thanks Tim!
                  Working...