Expect Script to install the Zabbix-Agent on all Linux Servers

Por: Dr. Delso Bonfante | May 22, 2015

Expect Script to install the Zabbix-Agent on all Linux Servers

Zabbix is the ultimate enterprise-level software designed for real-time monitoring of millions of metrics collected from tens of thousands of servers, virtual machines and network devices.
Zabbix is Open Source and comes at no cost.

Expect has regular expression pattern matching and general program capabilities, allowing simple scripts to intelligently control programs such as telnet, ftp, and ssh, all of which lack a programming language, macros, or any other program mechanism.

In this article I will demonstrate how to create a expect script to automate zabbix-agent install in all servers at once.

Install expect

# yum install expect expectk

Create dir

# mkdir -p /opt/adm

#

cd /opt/adm

Create a list of your hosts to install

# vi hosts.txt

192.168.0.1
192.168.0.2
192.168.0.3

Create the expect Script

If you want to test in 1 host uncomment the line #set host 192.168.0.1 and comment set host [lindex $argv 0]

# vi zabbix_install.expect

#!/usr/bin/expect

set timeout 5
set user "root"

set host 192.168.0.1

set host [lindex $argv 0]
set pass "Your_Password"
log_file resultado.log

spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${user}@${host}"
expect "assword"
send "$passr"
expect "${user}@"
send "whoamir"

Customizing

send "rpm -ivh http://repo.zabbix.com/zabbix/2.2/rhel/6/x86_64/zabbix-release-2.2-1.el6.noarch.rpm r"
send "yum install zabbix zabbix-agent -y r"
send "mv /etc/zabbix/zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf_original r"
send "echo Hostname=$(hostname) > /etc/zabbix/zabbix_agentd.confr"
send "echo Server=monitoramento.diegoluisi.eti.br >> /etc/zabbix/zabbix_agentd.confr"
send "echo ServerActive=monitoramento.diegoluisi.eti.br >> /etc/zabbix/zabbix_agentd.confr"
send "echo EnableRemoteCommands=1 >> /etc/zabbix/zabbix_agentd.confr"
send "echo Timeout=30 >> /etc/zabbix/zabbix_agentd.confr"
send "echo LogFileSize=0 >> /etc/zabbix/zabbix_agentd.confr"
send "echo LogFile=/var/log/zabbix/zabbix_agentd.log >> /etc/zabbix/zabbix_agentd.confr"
send "echo PidFile=/var/run/zabbix/zabbix_agentd.pid >> /etc/zabbix/zabbix_agentd.confr"
send "echo '############# www.diegoluisi.eti.br #############' >> /etc/zabbix/zabbix_agentd.confr"
send "iptables -A INPUT -p tcp -m tcp --dport 10050 -j ACCEPT r"
send "iptables-save"
send "/etc/init.d/iptables restart"
send "chkconfig zabbix-agent on r"
send "/etc/init.d/zabbix-agent restart r"
send "exitr"
send "exitr"
interact

Execute in all hosts of your list

for i in $(cat hosts.txt);do ./zabbix_install.expect $i;done

Captura de Tela 2015-05-22 às 13.53.30


LOREM

dluisi

🚀 Enthusiastic about the DevOps culture and focused on Open Source technologies, I am passionate about automation, with 20 years of experience as a SysAdmin, currently working as an SRE, always open to learning new technologies and procedures to improve every day more as a professional. 👀 I’m interested in Open Source projects (Linux, CNCF, GitOps and Tools) 📚 I’m currently increasing my skills on AWS, K8S, Python, Backstage and some automations tools like Argo (CD, Autopilot, Events, Rollouts and Workflows). 🌱 Goals: Increase my programming skills. 💞 I love old and good Rock and Roll, on weekends traveling on a Harley Davidson.