Tuesday, 17 June 2014

source、sh、bash、./执行脚本的区别

1、source命令用法:
  source FileName
  作用:在当前bash环境下读取并执行FileName中的命令。该filename文件可以无"执行权限"
   注:该命令通常用命令“.”来替代。
   如:source .bash_profile
       . .bash_profile两者等效。
   source(或点)命令通常用于重新执行刚修改的初始化文档。
   source命令(从 C Shell 而来)是bash shell的内置命令。
   点命令,就是个点符号,(从Bourne Shell而来)。
2、sh和bash命令用法:
    sh FileName
    bash FileName
    作用:在当前bash环境下读取并执行FileName中的命令。该filename文件可以无"执行权限"
    注:两者在执行文件时的不同,是分别用自己的shell来跑文件。
  sh使用“-n”选项进行shell脚本的语法检查,使用“-x”选项实现shell脚本逐条语句的跟踪,
  可以巧妙地利用shell的内置变量增强“-x”选项的输出信息等。
3、./的命令用法:
    ./FileName
    作用:打开一个子shell来读取并执行FileName中命令。
    注:运行一个shell脚本时会启动另一个命令解释器.
        每个shell脚本有效地运行在父shell(parent shell)的一个子进程里.
           这个父shell是指在一个控制终端或在一个xterm窗口中给你命令指示符的进程.
        shell脚本也可以启动他自已的子进程.
           这些子shell(即子进程)使脚本并行地,有效率地地同时运行脚本内的多个子任务.
shell的嵌入命令:
: 空,永远返回为true
.   从当前shell中执行操作
break 退出for、while、until或case语句
cd 改变到当前目录
continue 执行循环的下一步
echo 反馈信息到标准输出
eval 读取参数,执行结果命令
exec 执行命令,但不在当前shell
exit 退出当前shell
export 导出变量,使当前shell可利用它
pwd 显示当前目录
read 从标准输入读取一行文本
readonly 使变量只读
return 退出函数并带有返回值
set 控制各种参数到标准输出的显示
shift 命令行参数向左偏移一个
test 评估条件表达式
times 显示shell运行过程的用户和系统时间
trap 当捕获信号时运行指定命令
ulimit 显示或设置shell资源
umask 显示或设置缺省文件创建模式

Wednesday, 5 June 2013

Zabbix add host

Zabbix add host 

1. Login to Zabbix with admin, http://112.121.148.102/zabbix
2. Go to Configuration | Hosts
3. Click Create Host
4. Set Name, IP, Groups and add relative templates, then save it.
 
By default, zabbix windows template did not include network interface monitor, we need to add internet monitor by ourselves manually, Following is the steps to add internet monitor
 
1. check and have a look the monitor items you can add
run cmd.exe, type in typeperf -qx > c:\perfcounters.txt
open perfcounters.txt, you can get all monitor items you can add
the other way, click Start | Administrative Tools | Performance Monitor | Performance | Monitoring Tools | Performance Monitor, (following take internet as sample)
Click Performance Monitor | add, select Network Interface and Instances of selected object, Click add, then Click OK.
Select and right click, select Properties, under Data label you can get the monitor items like following size:
\Network Interface(Broadcom BCM5716C NetXtreme II GigE [NDIS VBD Client] _36)\Bytes Sent/sec
2. Add this monitor item to zabbix_agentd.win.conf of the Windows server you want to monitor:
PerfCounter = NetOut,"\Network Interface(Broadcom BCM5716C NetXtreme II GigE [NDIS VBD Client] _36)\Bytes Sent/sec",60
3. Test whether can get data from agent server and zabbix server server,
test at windows agent server
run cmd.exe , and type in 
C:\>zabbix\bin\win64\zabbix_agentd.exe -c "c:\zabbix\conf\zabbix_agentd.win.conf
" -t NetIn
 test at zabbix server server
root@zabbix-server:/etc/zabbix# /usr/bin/zabbix_get -s 203.117.155.190 -p 10050 -k NetIn
757.566205
root@zabbix-server:/etc/zabbix#
4. Add monitor item to windows template;
Click Configuration | Host groups | templates, go to windows_template
Click Items, then click Create Item button, type in description, type, key, etc. then save it.
5. Go to Windows server, active this item.







 
 
 

Zabbix Agent installation

Zabbix Agent Installation on Ubuntu

1. Download agent package,
    #wget http://www.zabbix.com/downloads/1.8.5/zabbix_agents_1.8.5.linux2_6.amd64.tar.gz
2. Abstract agent package
    #tar zxvf zabbix_agents_1.8.5.linux2_6.amd64.tar.gz
3. Copy files to destination directory 
    #cp bin/zabbix_get /usr/bin/
    #cp bin/zabbix_sender /usr/bin/
    #cp sbin/zabbix_agent /usr/sbin/
    #cp sbin/zabbix_agentd /usr/sbin/  
4. create zabbix folder
    #mkdir /etc/zabbix
    #cp conf/zabbix_agent* /etc/zabbix/
5. configure agent, 
    #vi /etc/zabbix/zabbix_agentd.conf
    ****************************
    #LogFile=c:\win32\zabbix_agentd.log
    LogFile=/tmp/zabbix_agentd.log
    Hostname=mmot
    Server=192.168.1.103
    ServerPort=10051
    ListenPort=10050
    ListenIP=192.168.1.112
    StartAgents=3
    Timeout=3
    ****************************
6. create /etc/init.d/zabbix-agent
    ****************************
#! /bin/sh
### BEGIN INIT INFO
# Provides:          zabbix-agent
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start zabbix-agent daemon
### END INIT INFO

set -e

NAME=zabbix_agentd
DAEMON=/usr/sbin/$NAME
DESC="Zabbix agent"

test -x $DAEMON || exit 0

DIR=/var/run/zabbix-agent
PID=$DIR/$NAME.pid
RETRY=15

if test ! -d "$DIR"; then
  mkdir "$DIR"
  chown -R zabbix:zabbix "$DIR"
fi

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

# define LSB log_* functions.
. /lib/lsb/init-functions

case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
        start-stop-daemon --oknodo --start --pidfile $PID \
          --exec $DAEMON >/dev/null 2>&1
    case "$?" in
        0) log_end_msg 0 ;;
        *) log_end_msg 1; exit 1 ;;
    esac
        ;;
  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
        start-stop-daemon --oknodo --stop --exec $DAEMON --retry $RETRY
    case "$?" in
        0) log_end_msg 0 ;;
        *) log_end_msg 1; exit 1 ;;
    esac
        ;;
  status)
   ls -l /proc/`cat $PID`/exe &> /dev/null
   if [ $? -eq 0 ]; then
       echo "$DESC is running"
       exit 0
   else
       echo "$DESC is NOT running"
       exit 1
   fi
   ;;
  restart|force-reload)
        $0 stop
        $0 start
        ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0

    ****************************
7. start zabbix agent 
    #/etc/init.d/zabbix-agent start
8. Open iptables 10050 port, and restart iptables


The second way to install zabbix agent on ubuntu server
1.  aptitude install zabbix-agent -y
2.  alter following items of zabbix_agentd.conf under /etc/zabbix
Server=112.121.148.102  #change to your ip of zabbix server
ServerPort=10051             # zabbix server listening port
Hostname=ubuntu94         #hostname of zabbix agent
ListenPort=10050               #zabbix agent listening port
ListenIP=203.116.138.94    #local public ip of zabbix agent
LogFile=/var/log/zabbix-agent/zabbix_agentd.log

3.  restart zabbix agent 
# /etc/init.d/zabbix-agent restart
4.  open ip of zabbix server and port 10050 of iptables, and restart iptables
vi /etc/sysconfig/iptables
add following two lines:
-A INPUT -s 112.121.148.102 -p tcp -m tcp --dport 10050 -j ACCEPT
-A INPUT -s 112.121.148.102 -p udp -m udp --dport 10050 -j ACCEPT
restart iptables
#/etc/init.d/iptables restart
 






Zabbix Agent installation on Windows server

1. Download zabbix agent package file from http://www.zabbix.com/download.php
2. Create a New Folder named Zabbix under D: , extract zabbix agent package to D:\Zabbix. (You can put everywhere what you want)
3. Setting config file zabbix_agentd.win.conf under D:\Zabbix\conf
    Server=112.121.148.102
    ListenPort=10050
    Hostname=Windows host
4. Install Zabbix agent by running following commands in CMD
    D:\Zabbix\bin\win64>zabbix_agentd.exe -c "D:\Zabbix\conf\zabbix_agentd.win.conf" --install
5. Add it to Windows serives
    D:\Zabbix\bin\win64>zabbix_agentd.exe -c "D:\Zabbix\conf\zabbix_agentd.win.conf" -s 
6. If you want to uninstall it from services, Can run following commands
    D:\Zabbix\bin\win64>zabbix_agentd.exe -c "D:\Zabbix\conf\zabbix_agentd.win.conf" -x
    D:\Zabbix\bin\win64>zabbix_agentd.exe -c "D:\Zabbix\conf\zabbix_agentd.win.conf" -d
    #-c: assign the directory of config file
    #-i:  install zabbix agent
    #-s: start zabbix agent
    #-x: stop running of zabbix agent
    #-d: uninstall zabbix agent                     
      

7. Start zabbix agent
    D:\Zabbix\bin\win64>zabbix_agentd.exe -c "D:\Zabbix\conf\zabbix_agentd.win.conf" --start
8. Open both tcp and udp for 10050 of the server firewall
9. Add this server to Zabbix server monitoring.