Techy Findings

Just another Geek trying to be special

Posts tagged script

8 notes

Getting to grips with SNMP on Ubuntu

This post has an install script towards the bottom which will install SNMP Daemon and its required configuration for you. That feat is the result of many hours of work. The BASH scripting only took an hour or two to knock together, the main effort was getting SNMP working with no errors with both IPv4 and IPv6 support; MIB support and SNMP Traps sent for notification of events.

I have tested the script using ubuntu 11.04 and more recently with revision 1.1 of the scrpt I have tested with ubuntu 8.04.

NOTE: With NET-SNMP version 5.4.1 on ubuntu 8.0.4 I did notice that the only way to remove the error “getaddrinfo:  Name or service not known” on startup was to remove the IPv6 parameters from the listening address, IPv6 lookups do work on 8.0.4 but you do have to live with some early immature code issues.

I suggest you copy the script below and paste into a file on your ubuntu  system and make sure you chmod the file to make it executable from root;

chmod 755 <script-name>

should do it.

If you have a number of machines to install SNMP on, I suggest you change the defaults towards the top of the script to match your network and environment requirements this will make for a simpler and faster install.

#!/bin/bash
#
# SNMP install
#by Paul Miller
#Revisions:
#1.0 - Initial
#1.1 - corrections after testing with ubuntu 8.04 LTS


#CHANGE DEFAULT script variables here before running

def_READ_ONLY_SECRET=”public”
def_READ_WRITE_SECRET=”private”
def_IPv4_SUBNET=”192.168.0.0/24”
def_IPv6_SUBNET=”2001:470:812c::/64”
def_SYS_LOC=”New Zealand”
def_SYS_CONT_NAME=”Paul Miller”
def_SYS_CONT_EMAIL=”idkpmiller@sip2serve.com”
def_TRAP_COMMUNITY=”idknet”
def_TRAP_RECEIVER=”nms.sip2serve.com” # IP or FQDN of a SNMP TRAP recievering
server


#====END of User Varables==================

#check current user is root
(( `id -u` )) && echo “Must be ran as root, try prefixxing with sudo.” && exit 1

#clear the screen
clear

# Get Input from User
echo “Capture User Options:”
echo “=====================”
echo “Please answer the following questions.”
echo “Hitting return will continue with the default option”
echo
echo


read -p “System Location [$def_SYS_LOC]: ” -e t1
if [ -n “$t1” ]; then def_SYS_LOC=”$t1”;fi

read -p “System Contact Name [$def_SYS_CONT_NAME]: ” -e t1
if [ -n “$t1” ]; then def_SYS_CONT_NAME=”$t1”;fi

read -p “System Contact Email [$def_SYS_CONT_EMAIL]: ” -e t1
if [ -n “$t1” ]; then def_SYS_CONT_EMAIL=”$t1”;fi

read -p “Trap Community String [$def_TRAP_COMMUNITY]: ” -e t1
if [ -n “$t1” ]; then def_TRAP_COMMUNITY=”$t1”;fi

read -p “NMS or Trap Reciever Address (IP or FQDN) [$def_TRAP_RECEIVER]: ” -e t1
if [ -n “$t1” ]; then def_TRAP_RECEIVER=”$t1”;fi

read -p “IPv4 subnets that can access SNMP on System [$def_IPv4_SUBNET]: ” -e t1
if [ -n “$t1” ]; then def_IPv4_SUBNET=”$t1”;fi

read -p “IPv6 subnets that can access SNMP on System [$def_IPv6_SUBNET]: ” -e t1
if [ -n “$t1” ]; then def_IPv6_SUBNET=”$t1”;fi

read -p “Read Only Community String [$def_READ_ONLY_SECRET]: ” -e t1
if [ -n “$t1” ]; then def_READ_ONLY_SECRET=”$t1”;fi

read -p “Read Write Community String [$def_READ_WRITE_SECRET]: ” -e t1
if [ -n “$t1” ]; then def_READ_WRITE_SECRET=”$t1”;fi

cat «EOF
=====================
The Answers provided:
=====================
System Location:       $def_SYS_LOC
System Contact:        $def_SYS_CONT_NAME
System Contact Email:  $def_SYS_CONT_EMAIL
RO Community String:   $def_READ_ONLY_SECRET
RW Community String    $def_READ_WRITE_SECRET
Trap Community String: $def_TRAP_COMMUNITY
Trap Reciever Address: $def_TRAP_RECEIVER
IPv4 Subnet ACL:       $def_IPv4_SUBNET
IPv6 Subnet ACL:       $def_IPv6_SUBNET

EOF

read -p “Press Y to continue with Install.” -n 1
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    echo “”
    echo “Goodbye!”
    exit 1
fi


# update system
echo “”
echo “###############################################”
echo “update system”
apt-get -qq update

# install requirements
echo “install requirements”
apt-get -y -qq install snmp
apt-get -y -qq install snmp-mibs-downloader
apt-get -y -qq install snmpd
apt-get -y -qq install libsnmp-base

# Configure SNMP
echo “Configure SNMP”
echo “###############”


cat «EOF > /etc/snmp/snmp.conf
#
# As the snmp packages come without MIB files due to license reasons, loading
# of MIBs is disabled by default. If you added the MIBs you can reenable
# loaging them by commenting out the following line.
#mibs :
EOF

echo “/etc/snmp/snmp.conf  - DONE.”
#==========================================

cat «EOF > /etc/snmp/snmpd.conf
############### Generic config information ###############
agentAddress udp:161,udp6:161
sysLocation    $def_SYS_LOC
sysServices    72
sysContact     $def_SYS_CONT_NAME <$def_SYS_CONT_EMAIL>
rocommunity $def_READ_ONLY_SECRET  $def_IPv4_SUBNET
rocommunity6 $def_READ_ONLY_SECRET  $def_IPv6_SUBNET
rwcommunity $def_READ_WRITE_SECRET
rwcommunity6 $def_READ_WRITE_SECRET
trapcommunity $def_TRAP_COMMUNITY
trap2sink $def_TRAP_RECEIVER

############### Common config directives ###############
disk /
disk /var
disk /usr
disk /tmp
swap 16000
#linkUpDownNotifications yes
#defaultMonitors yes
master agentx

############### Node specific config directives ###############

EOF

echo “/etc/snmp/snmpd.conf  - DONE.”
#==========================================


cat «EOF > /etc/snmp/snmptrapd.conf
#
# PLEASE: read the snmptrapd.conf(5) manual page as well!
#
TRAPDRUN=yes
EOF

echo “/etc/snmp/snmptrapd.conf  - DONE.”
#==========================================

cat «EOF > /etc/default/snmpd
SNMPDRUN=yes
SNMPDOPTS=’-Lsd -Lf /dev/null -u snmp -g snmp -I -smux -p /var/run/snmpd.pid’
TRAPDRUN=no
TRAPDOPTS=’-Lsd -p /var/run/snmptrapd.pid’
SNMPDCOMPAT=yes
EOF

echo “/etc/default/snmpd  - DONE.”
#==========================================

echo ‘###############’
echo ‘Starting SNMP  ‘
echo ‘###############’

/etc/init.d/snmpd restart
sleep 5

if [ “$(pidof snmpd)” ]
   then
       clear
    cat «EOF
Installation and Configuration of snmp
was successful.

INSTRUCTIONS
============
you can test snmp by using the following commands
for IPv4 systems:
snmpwalk -c public -v 2c localhost sysname

for IPv6 systems:
snmpwalk -c public -v 2c udp6:::1 sysname
EOF

else
      echo “snmpd FAILED! to start”
      echo “Not sure what went wrong.”
fi

exit

Revision: 1.0 - Initial post

             1.1 - corrections after testing with ubuntu 8.04

Filed under bash script ubuntu snmp IPv4 IPv6 Traps NMS community snmpd snmptrapd

5 notes

Zabbix install on Ubuntu 11.10

I have put a script together that allows me to install a specified version of Zabbix on a Ubuntu server, if you choose not to install the server the script assumes you want to install the Zabbix agent. It also provides an option to compile with IPv6 support.

Although there is some error checking, they can always be more added. If you decide to improve the script further please leave a comment with a link to the new improved version.

It is possibly that the CODE section of this blog provider mucks up some of the special formatting characters, if you have problems check what you see on this page to what has been copied into your script file.

As usual make sure you chmod +x the script file and I suggest you run this with root privelidges.

#!/bin/bash

#TODO
# error checking always needs improving


#Adapted from script found online
# CHANGES by Paul Miller:
#enhanced to offer IPv6 support
#Changed to support mysql
#added option for version support
#server or agent install
#added some error checking
####################################################


#Set Default script variables here before running

DATABASE=”mysql” # option ONLY mysql at present
IPv6=true # options true or false
VERSION=”1.8.8”
DB_USER=”zabbix”
DB_PASS=”zabb1x”
DB_HOST=”localhost”
SERVER_IP=”192.168.0.9” # IP of zabbix server for agents to communicate with.
SERVER_INSTALL=true # if false assumed to be agent only install

#====END of User Varables==================
IPv4_ADDR=`ifconfig  | grep ‘inet addr:’| grep -v ‘127.0.0.1’ | cut -d: -f2 | awk ‘{ print $1}’`
MYSQL=”$(which mysql)”
HOSTNAME=$(hostname -f)
clear

# Get Input from Users
echo “Capture User Options:”
echo “=====================”
echo “Please answer the following questions.”
echo “Hitting return will continue with the default option”
echo
echo
# Get_Server - Do they require a Server Install?
read -p “Install Zabbix Server? true/false [$SERVER_INSTALL]: ” -e t1
if [ -n “$t1” ]
then
  SERVER_INSTALL=”false”
  #Now we ASSUME that this is a Agent ONLY install
  # Get_Server_IP - What Version of Zabbix do they require?
  read -p “What is the Zabbix Server IP Address? [$SERVER_IP]: ” -e t1
  if [ -n “$t1” ]
  then
     SERVER_IP=”$t1”
  else
     SERVER_IP=”192.168.0.9”
  fi
  #====END Get_Server_IP
else
  SERVER_INSTALL=”true”
fi
#====END Get_Server

# Get_Version - What Version of Zabbix do they require?
read -p “Which version? [$VERSION]: ” -e t1
if [ -n “$t1” ]
then
  VERSION=”$t1”
else
  VERSION=”1.8.8”
fi
#====END Get_Version
 
# Get_IPv6 - Do they require IPv6 support?
read -p “Is support for IPv6 required? true/false [$IPv6]: ” -e t1
if [ -n “$t1” ]
then
  IPv6=”false”
else
  IPv6=”true”
fi
#====END Get_IPv6

# Step 1 ================================
# nothing to do for agent only installs
echo
echo “Installation Step 1 Started”

################
#updating system
################

apt-get -qq update
if [ $? -eq 0 ]; then
   echo “Step 1 completed successfully!”
else
   echo “Step 1 FAILED!”  
   exit
fi

# Step 2 ================================

if $SERVER_INSTALL ; then
   #####################
   #install requirements
   #####################
   echo
   echo “Installation Step 2 Started”

   if ! dpkg-query -W fping; then apt-get install -qq -y fping; fi
   if ! dpkg-query -W apache2; then apt-get install -qq -y apache2; fi
   if ! dpkg-query -W php5; then apt-get install -qq -y php5; fi
   if ! dpkg-query -W php5-gd; then apt-get install -qq -y php5-gd; fi
   if ! dpkg-query -W libsnmp-dev; then apt-get install -qq -y libsnmp-dev; fi
   if ! dpkg-query -W libcurl4-openssl-dev; then apt-get install -qq -y libcurl4-openssl-dev; fi
   if ! dpkg-query -W libapache2-mod-php5; then apt-get install -qq -y libapache2-mod-php5; fi
   if ! dpkg-query -W libiksemel-dev; then apt-get install -qq -y libiksemel-dev; fi
   if ! dpkg-query -W libssh2-1-dev; then apt-get install -qq -y libssh2-1-dev; fi
   if ! dpkg-query -W libopenipmi-dev; then apt-get install -qq -y libopenipmi-dev; fi
   if ! dpkg-query -W libmysqlclient-dev; then apt-get install -qq -y libmysqlclient-dev; fi
   if ! dpkg-query -W mysql-server; then apt-get install -qq -y mysql-server; fi
   if ! dpkg-query -W libapache2-mod-auth-mysql; then apt-get install -qq -y libapache2-mod-auth-mysql; fi
   if ! dpkg-query -W php5-mysql; then apt-get install -qq -y php5-mysql; fi

fi
if ! dpkg-query -W build-essential; then apt-get install -qq -y build-essential; fi

echo “Step 2 completed successfully!”

# Step 3 ================================

#######################
#Initial Database setup
#######################
echo
echo “Installation Step 3 Started”

# create zabbix system user
adduser zabbix —no-create-home —system —group —disabled-password —shell /bin/false —quiet
if [ $? -eq 0 ]; then
   echo “   User zabbix created”
else
   echo “   FAILED to create User zabbix”
   exit 1
fi

if $SERVER_INSTALL ; then

   if [ “$DATABASE” == “mysql” ]; then
      echo -n “   Input the MySQL admin user name: “
      read -e MySQLADMIN

      echo -n “   Input the MySQL admin user password: “
      read -e MySQLADMINPASS

      $MYSQL -u$MySQLADMIN -p$MySQLADMINPASS -Bse ‘CREATE DATABASE zabbix;’
      $MYSQL -u$MySQLADMIN -p$MySQLADMINPASS -Bse “GRANT ALL ON zabbix.* TO zabbix@localhost;”
      echo “   mysql database and user created”
    
   else
      echo “postgresql initial DB setup”
      #postgresql initial DB setup

      echo -n “Input the postgre user name for this database: “
      read -e DB_USER

      echo -n “Input the MySQL admin user password: “
      read -e MySQLADMINPASS

   fi

   echo “Step 3 completed successfully!”
fi



# Step 4 ================================
################
#Zabbix download
################
echo
echo “Installation Step 4 Started”

cd /tmp/

DIRECTORY=/tmp/install
if [ ! -d “$DIRECTORY” ]; then
    mkdir /tmp/install
fi

cd /tmp/install
echo ” temporary install directory created”
echo ” downloading zabbix source”

if ! [ -e “zabbix-$VERSION.tar.gz” ]
then
  wget -nv http://prdownloads.sourceforge.net/zabbix/zabbix-$VERSION.tar.gz
  echo “   downloaded zabbix source”
else
  echo “   zabbix source of correct version already exists”
fi

echo ” untar zabbix source”
tar zxf zabbix-$VERSION.tar.gz

echo ” prepare directory and file permissions”
chmod -R 777 /tmp/install/*
cd /tmp/install/zabbix-$VERSION
chmod +x ./configure

if $SERVER_INSTALL ; then
   # DB integration
   echo ” load mysql with provided schemas”
   cd /tmp/install/zabbix-$VERSION/create/schema
   cat mysql.sql | mysql -u$MySQLADMIN -p$MySQLADMINPASS zabbix
   cd ../data
   cat data.sql | mysql -u$MySQLADMIN -p$MySQLADMINPASS zabbix
   cat images_mysql.sql | mysql -u$MySQLADMIN -p$MySQLADMINPASS zabbix
fi

echo ” prepare compile build options”
if $SERVER_INSTALL ; then
   #Server DB and other build options
   build_opts=” —prefix=/usr —mandir=\${prefix}/share/man —infodir=\${prefix}/share/info “

   if $IPv6 ; then
      build_opts=” —enable-ipv6 $build_opts”
   fi

   # —with-mysql
   build_opts=” —with-mysql $build_opts”

   cd /tmp/install/zabbix-$VERSION
   echo “Using configure set like this: “
   echo “configure —quiet —enable-server —with-net-snmp —with-libcurl —with-openipmi —with-jabber —with-ssh2 —enable-agent $build_opts”

   echo “”
   ./configure —quiet —enable-server —with-net-snmp —with-libcurl —with-openipmi —with-jabber —with-ssh2 —enable-agent $build_opts
else
   #Agents only build options
   build_opts=” —prefix=/usr —mandir=\${prefix}/share/man —infodir=\${prefix}/share/info “

   if $IPv6 ; then
      build_opts=” —enable-ipv6 $build_opts”
   fi
   cd /tmp/install/zabbix-$VERSION
   echo “Using configure set like this: “
   echo “configure —quiet —enable-agent $build_opts”

   echo “”
   ./configure —quiet —enable-agent $build_opts

fi


echo “   Ready to compile”
cd /tmp/install/zabbix-$VERSION

make -s install
if [ $? -eq 0 ]; then
   echo “Step 4 completed successfully!”
else
   echo “Step 4 Compile FAILED!”
   exit
fi

sleep 5

#step 5 for Server installations
if $SERVER_INSTALL ; then
   #############################
   #Zabbix $VERSION installation
   #FRONTEND installation
   #############################
   echo “Step 5 FRONTEND installation”

   sed -i.backup -e “s/post_max_size = 8M/post_max_size = 32M/g” /etc/php5/apache2/php.ini
   sed -i.backup -e “s/max_execution_time = 30/max_execution_time = 600/g” /etc/php5/apache2/php.ini
   sed -i.backup -e “s/max_input_time = 60/max_input_time = 600/g” /etc/php5/apache2/php.ini
   sed -i,backup -e ‘/date.timezon/a\date.timezone = “Pacific/Auckland”’ /etc/php5/apache2/php.ini
   cd /tmp/install/zabbix-$VERSION/frontends/php
   sleep 5
   echo “   Make web directory”
   DIRECTORY=/var/www/zabbix
   if [ ! -d “$DIRECTORY” ]; then
      mkdir /var/www/zabbix
   fi
   echo “   Copy zabbix web frontend to web directory”
   cp -a . /var/www/zabbix
   echo “   Change the permissions to default apache2”
   chown www-data:www-data -R /var/www/zabbix


   cat «EOF > /etc/apache2/sites-available/zabbix
<VirtualHost /zabbix>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/zabbix
        <Directory />
                Options FollowSymLinks Indexes MultiViews
                AllowOverride None
        </Directory>
</VirtualHost>
EOF

   echo “   Zabbix $VERSION installation”
fi

#step 5 for agents only they join here…

ln -s /usr/bin/fping /usr/sbin/fping
if $IPv6 ; then
   ln -s /usr/bin/fping6 /usr/sbin/fping6
fi

DIRECTORY=/etc/zabbix
if [ ! -d “$DIRECTORY” ]; then
   mkdir $DIRECTORY
fi

DIRECTORY=/var/log/zabbix
if [ ! -d “$DIRECTORY” ]; then
   mkdir $DIRECTORY
   chown zabbix:zabbix -R $DIRECTORY
   chmod 766 $DIRECTORY
fi

DIRECTORY=/var/run/zabbix
if [ ! -d “$DIRECTORY” ]; then
    mkdir $DIRECTORY
    chown zabbix:zabbix -R $DIRECTORY
    chmod 766 $DIRECTORY
fi

cp /tmp/install/zabbix-$VERSION/misc/conf/zabbix_agentd.conf /etc/zabbix

#check for server install
if $SERVER_INSTALL ; then
   cp /tmp/install/zabbix-$VERSION/misc/conf/zabbix_server.conf /etc/zabbix
   sed -i.backup -e “s/DBUser=root/DBUser=$DB_USER/g” -e “s|/tmp/zabbix_server.log|/var/log/zabbix/zabbix_server.log|g” -e “s|# PidFile=/tmp/zabbix_server.pid|PidFile=/var/run/zabbix/zabbix_server.pid|g” /etc/zabbix/zabbix_server.conf
fi
sed -i.backup -e “s|/tmp/zabbix_agentd.log|/var/log/zabbix/zabbix_agentd.log|g” -e “s|# PidFile=/tmp/zabbix_agentd.pid|PidFile=/var/run/zabbix/zabbix_agentd.pid|g” /etc/zabbix/zabbix_agentd.conf

chown zabbix:zabbix -R /etc/zabbix

cp /tmp/install/zabbix-$VERSION/misc/init.d/debian/zabbix* /etc/init.d/

#check for server install
if $SERVER_INSTALL ; then
   sed -i.backup -e “s|/usr/local/sbin/|/usr/sbin/|” /etc/init.d/zabbix-server
   chmod 775 /etc/init.d/zabbix-server
   update-rc.d zabbix-server defaults
   echo “   Starting the zabbix server”
   /etc/init.d/zabbix-server start
   echo “   Restarting Apache for changes to take effect”
   /etc/init.d/apache2 restart
   sleep 5
   if [ “$(pidof zabbix_server)” ]
   then
      echo “Server Installation Complete!”
      echo “zabbix can be found at: “
      echo “http://$IPv4_ADDR/zabbix”
      echo “  Login:  admin”
      echo “  Passwd: zabbix”
   else
      echo “Installation FAILED!”
      echo “zabbix server process is NOT running.”
      echo “Not sure what went wrong.”
   fi
else
   sed -i.backup -e “s|/tmp/zabbix_agentd.log|/var/log/zabbix/zabbix_agentd.log|g” -e “s|# PidFile=/tmp/zabbix_agentd.pid|PidFile=/var/run/zabbix/zabbix_agentd.pid|g” -e “s|Server=127.0.0.1|Server=$SERVER_IP|g” -e “s|Hostname=Zabbix server|Hostname=$HOSTNAME|g” /etc/zabbix/zabbix_agentd.conf

fi

sed -i.backup -e “s|/usr/local/sbin/|/usr/sbin/|” /etc/init.d/zabbix-agent
chmod 775 /etc/init.d/zabbix-agent
update-rc.d zabbix-agent defaults
echo “   Starting the zabbix agent”
/etc/init.d/zabbix-agent start
sleep 5

if [ “$(pidof zabbix_agentd)” ]
   then
      #cleaning up
      rm -rf /tmp/install
      echo “Agent Installation Complete!”
   else
      echo “Agent Installation FAILED!”
      echo “zabbix agent process is NOT running.”
      echo “Not sure what went wrong.”
   fi

exit

Revision History

  • First release 30/12/2011

Filed under Bash Script ubuntu zabbix zabbix-agent zabbix_agent install howto 11.10

7 notes

Webmin installation script for Ubuntu 11.10

Ok, so its not rocket science to install webmin on Ubuntu, however as I spend a reasonable amount of time building up servers to try new applications or test particular versions of applications that I already use, I find I spend a fair amount of time repeating the same boring tasks; one of them being installing Webmin.

To use this script sudo in root using

sudo su

Make sure the script has the execute permision flag set you can simply ‘cd’ to the directory where you copied the script and type:

chmod +x webmin_install.sh

run the script by typing

./webmin_install.sh

This script will install the latest version of webmin on your machine.

Hmm… I cannot see how to attach the script file to this post, so instead I have copied the contents below, the details above assume you copy the script below into a file called webmin_install.sh

#!/bin/bash
#
# webmin install


# update system

echo ‘###############’
echo ‘update system’
echo ‘###############’
apt-get update

# install requirements
echo ‘###############’
echo ‘install requirements’
echo ‘###############’

apt-get install -y perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl apt-show-versions python

#update repo
echo ‘###############’
echo ‘   Repo setup  ‘
echo ‘###############’

echo “deb http://download.webmin.com/download/repository sarge contrib” » /etc/apt/sources.list
echo “deb http://webmin.mirror.somersettechsolutions.co.uk/repository sarge contrib” » /etc/apt/sources.list

#Install GPG key
echo ‘###############’
echo ‘  Install GPG  ‘
echo ‘###############’
cd /root
wget http://www.webmin.com/jcameron-key.asc
apt-key add jcameron-key.asc

#Download and Install webmin
echo ‘###############’
echo ‘Install Webmin ‘
echo ‘###############’
apt-get update
apt-get install -y webmin


Filed under ubuntu webmin install script bash