美文网首页
Zabbix-agent添加到系统服务步骤

Zabbix-agent添加到系统服务步骤

作者: 阿乐_822e | 来源:发表于2022-01-27 16:44 被阅读0次

在内网中安装Zabbix-agent时,通常使用源码安装,这时要将Zabbix-agent添加到系统服务,当系统重启后让它自动启动

1、编写zabbix_agentd服务脚本

$ vi /etc/init.d/zabbix_agentd

#!/bin/bash
#
# chkconfig: 2345 80 05
# description:  Starts and stops Zabbix Agent using chkconfig
#               Tested on Fedora Core 2 - 5
#               Should work on all Fedora Core versions
#
# @name:    zabbix_agentd
# @author:  Alexander Hagenah <hagenah@topconcepts.com>
# @created: 18.04.2006
#
# Modified for Zabbix 2.0.0
# May 2012, Zabbix SIA
#
# Source function library.
. /etc/init.d/functions

# Variables
# Edit these to match your system settings

    # Zabbix-Directory
    BASEDIR=/usr/local/zabbix-agent

    # Binary File
    BINARY_NAME=zabbix_agentd

    # Full Binary File Call
    FULLPATH="$BASEDIR/sbin/$BINARY_NAME -c $BASEDIR/conf/zabbix_agentd.conf"

    # PID file
    PIDFILE=/tmp/$BINARY_NAME.pid

    # Establish args
    ERROR=0
    STOPPING=0

#
# No need to edit the things below
#

# application checking status
if [ -f $PIDFILE  ] && [ -s $PIDFILE ]
    then
    PID=`cat $PIDFILE`

    if [ "x$PID" != "x" ] && kill -0 $PID 2>/dev/null && [ $BINARY_NAME == `ps -e | grep $PID | awk '{print $4}'` ]
    then
        STATUS="$BINARY_NAME (pid `pidof $APP`) running.."
        RUNNING=1
    else
        rm -f $PIDFILE
        STATUS="$BINARY_NAME (pid file existed ($PID) and now removed) not running.."
        RUNNING=0
    fi
else
    if [ `ps -e | grep $BINARY_NAME | head -1 | awk '{ print $1 }'` ]
        then
        STATUS="$BINARY_NAME (pid `pidof $APP`, but no pid file) running.."
    else
        STATUS="$BINARY_NAME (no pid file) not running"
    fi
    RUNNING=0
fi

# functions
start() {
    if [ $RUNNING -eq 1 ]
        then
        echo "$0 $ARG: $BINARY_NAME (pid $PID) already running"
    else
        action $"Starting $BINARY_NAME: " $FULLPATH
        touch /var/lock/subsys/$BINARY_NAME
    fi
}

stop() {
    echo -n $"Shutting down $BINARY_NAME: "
    killproc $BINARY_NAME
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$BINARY_NAME
    RUNNING=0
}


# logic
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status $BINARY_NAME
        ;;
    restart)
        stop
        sleep 10
        start
        ;;
    help|*)
        echo $"Usage: $0 {start|stop|status|restart|help}"
        cat <<EOF

            start       - start $BINARY_NAME
            stop        - stop $BINARY_NAME
            status      - show current status of $BINARY_NAME
            restart     - restart $BINARY_NAME if running by sending a SIGHUP or start if not running
            help        - this screen

EOF
    exit 1
    ;;
esac

exit 0

PS: 这里的“BASEDIR”要换成自己zabbix-agent的实际安装目录

2、把zabbix_agentd添加到系统启动并查看服务状态

$ chmod +x /etc/init.d/zabbix_agentd
$ chkconfig --add zabbix_agentd
$ chkconfig  zabbix_agentd on
$ chkconfig --list zabbix_agentd
$ systemctl restart zabbix_agentd
$ systemctl status zabbix_agentd

相关文章

网友评论

      本文标题:Zabbix-agent添加到系统服务步骤

      本文链接:https://www.haomeiwen.com/subject/noeahrtx.html