Zabbix on macOS(Draft)

Zabbix on Mac

For some reasons, I needed to prepare and run a monitoring service(both server and agent) which runs on Mac (and Linux). I found Zabbix and Nagios are one of the best choices, and I chose Zabbix this time.

Per my quick check, it is very easy to setup an agent on macOS, but not so easy on server side. So I ended up setting up a Zabbix server and its related components(Web, DB servers) using Docker.

In Short,

  • Use Docker for Server (and related components, Web, DB Servers)
  • Use HomeBrew for Agent

Prerequisite

  • Homebrew
  • Docker for Mac

Configure and Run Zabbix Server using Docker

You can configure/setup Zabbix and its related components/containers individually, but I’d suggest to use Docker Compose to handle everything needed to run Docker Server/Service.

Prepare docker-compose.yaml file in the directory of your choice, then run containers using docker-compose.

docker-compose.yaml
version: '2'
services:
  zabbix-db:
    image: monitoringartist/zabbix-db-mariadb
    volumes:
      - zabbix-db-storage:/var/lib/mysql
      - backups:/backups
      - /etc/localtime:/etc/localtime:ro
    environment:
      - MARIADB_USER=zabbix
      - MARIADB_PASS=my_password
  zabbix-server:
    image: monitoringartist/zabbix-3.0-xxl:latest
    depends_on:
      - zabbix-db
    ports:
      - "80:80"
      - "10051:10051"
    volumes:
      - /etc/localtime:/etc/localtime:ro
    links:
      - zabbix-db:zabbix.db
    environment:
      - ZS_DBHost=zabbix.db
      - ZS_DBUser=zabbix
      - ZS_DBPassword=my_password
volumes:
  zabbix-db-storage:
    driver: local
  backups:
    driver: local
Run Zabbix server
$ docker-compose up -d # -d option indicates that the containers are run in detached mode/background

 

Install and Run Zabbix Agent on macOS

Install Zabbix Agent

Install Zabbix
$ brew install zabbix

This does install all the zabbix components, incl Server, Proxy, and Agent.
If you only need to install Zabbix Agent on a target system, you can do,

Install Zabbix Agent
$ brew install --agent-only zabbix # Zabbix 2.x
$ brew install --without-server-proxy zabbix  # Zabbix 3.x

Run Zabbix Agent

You can run Zabbix Agent simply by doing,

Run Zabbix Agent from Command Line
$ /usr/local/sbin/zabbix_agentd

But this is only for 1 time run, and we want it to be started automatically when the system is rebooted, so it’s better to prepare an init script for the agent.

With the init script, you can start/stop the agent manually, or from any service management systems, such as, initd, systemd, launchd.

Traditional Unix init script style
#!/bin/sh
 
agentd=/usr/local/sbin/zabbix_agentd
conf=/usr/local/etc/zabbix_agentd.conf
pid=/tmp/zabbix_agentd.pid
 
case $1 in
start)
    $agentd -c $conf;;
stop)
    kill -TERM `cat $pid`;;
restart)
    $0 stop
    sleep 10
    $0 start
    ;;
*)
    echo "Usage: $0 start|stop|restart"
    exit 1
esac

Or if you like it to be more macOS-ish(but too traditional/obsoleted actually tho),

StartupItems style (obsoleted)
#!/bin/sh
. /etc/rc.common
AGENTD=/usr/local/sbin/zabbix_agentd
CONF=/usr/local/etc/zabbix_agentd.conf
PIDFILE=/tmp/zabbix_agentd.pid
program="$0"
StartService() {
        $AGENTD -c $CONF
}
 
StopService() {
     kill -TERM $(GetPID $program)
}
RestartService() {
    StopService
    sleep 3
    StartService
}
 
RunService "$1"

 

Save the above script then run

start/stop zabbix_agentd using init script
# Assuming the saved script name is "zabbix_agent".
$ zabbix_agent start
$ zabbix_agent stop
$ zabbix_agent restart 

As described above, using a shell script to start/stop/restart a service, is not the officially recommended way for macOS, so if you like, please consider preparing plist and use launchctl[d].

com.zabbix.zabbix_agentd.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.zabbix.zabbix_agentd</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>SuccessfulExit</key>
    <false/>
    <key>UserName</key>
    <string>zabbix</string>
    <key>GroupName</key>
    <string>zabbix</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/sbin/zabbix_agentd</string>
        <string>-c</string>
        <string>/usr/local/etc/zabbix/zabbix_agentd.conf</string>
    </array>
</dict>
</plist>

 

 

References

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s