Skip to content

Commit fe56d06

Browse files
committed
Updated zabbix-agent to 3.0.1
Added ENV variables for custom config Added ENV variables for hostame Added ENV variables for host metadata
1 parent b88f24c commit fe56d06

File tree

13 files changed

+628
-311
lines changed

13 files changed

+628
-311
lines changed

Dockerfile

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
FROM centos:centos7
22
MAINTAINER Przemyslaw Ozgo linux@ozgo.info
33

4-
COPY foreground.patch /foreground.patch
5-
6-
ENV ZABBIX_VERSION=2.4.7
4+
ENV ZABBIX_VERSION=3.0.1 \
5+
ZABBIX_SERVER=127.0.0.1 \
6+
HOSTNAME=zabbix.agent \
7+
HOST_METADATA=zabbix.agent \
8+
CONFIG_FILE=/usr/local/etc/zabbix_agentd.conf
79

810
RUN \
911
yum clean all && yum makecache && \
10-
yum install --nogpgcheck -y svn automake gcc make && \
12+
yum install --nogpgcheck -y svn automake gcc make iproute && \
1113
svn co svn://svn.zabbix.com/tags/${ZABBIX_VERSION} /usr/local/src/zabbix && \
1214
cd /usr/local/src/zabbix && \
13-
svn patch /foreground.patch && \
1415
./bootstrap.sh && \
1516
./configure --enable-agent && \
1617
make install && \
@@ -20,10 +21,13 @@ RUN \
2021
rm -rf /usr/local/src/zabbix && \
2122
yum clean all
2223

23-
COPY start.sh /start.sh
24+
COPY container-files /
2425

25-
ENV ZABBIX_SERVER=127.0.0.1
26+
RUN \
27+
chown -R zabbix:wheel /usr/local/etc/
2628

27-
CMD /start.sh
29+
USER zabbix
2830

2931
EXPOSE 10050
32+
33+
ENTRYPOINT ["/bootstrap.sh"]

README.md

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
## Zabbix Agent Docker Image
22

33
[![Circle CI](https://circleci.com/gh/million12/docker-zabbix-agent/tree/master.svg?style=svg&circle-token=c12c61948ad16f75903de19a2001d982cd17809d)](https://circleci.com/gh/million12/docker-zabbix-agent/tree/master)
4+
[![GitHub Open Issues](https://img.shields.io/github/issues/million12/docker-zabbix-agent.svg)](https://github.com/million12/docker-zabbix-agent/issues)
5+
[![GitHub Stars](https://img.shields.io/github/stars/million12/docker-zabbix-agent.svg)](https://github.com/million12/docker-zabbix-agent)
6+
[![GitHub Forks](https://img.shields.io/github/forks/million12/docker-zabbix-agent.svg)](https://github.com/million12/docker-zabbix-agent)
7+
[![Stars on Docker Hub](https://img.shields.io/docker/stars/million12/zabbix-agent.svg)](https://hub.docker.com/r/million12/zabbix-agent)
8+
[![Pulls on Docker Hub](https://img.shields.io/docker/pulls/million12/zabbix-agent.svg)](https://hub.docker.com/r/million12/zabbix-agent)
9+
[![Docker Layers](https://badge.imagelayers.io/million12/zabbix-agent:latest.svg)](https://hub.docker.com/r/million12/zabbix-agent)
10+
11+
12+
[![Deploy to Docker Cloud](https://files.cloud.docker.com/images/deploy-to-dockercloud.svg)](https://cloud.docker.com/stack/deploy/?repo=https://github.com/million12/docker-zabbix-agent/tree/master)
413

514
This is a [million12/zabbix-agent](https://registry.hub.docker.com/u/million12/zabbix-agent/) docker image with Zabbix Agent. It's based on CentOS-7 official image.
615

7-
**Zabbix agent is running in foreground.**
16+
The Zabbix agent has been patched to read system informations from these directories:
17+
18+
`/data/proc` mapped from `/proc` on the real host
19+
`/data/dev` mapped from `/dev` on the real host
20+
`/data/sys` mapped from `/sys` on the real host
21+
22+
**Zabbix agent is running in foreground.**
23+
24+
## Build the image
25+
26+
docker build -t million12/zabbix-agent .
827

928
## ENV variables
1029

11-
#### ZABBIX_SERVER
12-
Default: `ZABBIX_SERVER=127.0.0.1`
13-
Change it to match your server
30+
`ZABBIX_SERVER` - Zabbix Server address
31+
`HOSTNAME` - hostname
32+
`HOST_METADATA` - the metadata value shared by all servers on the same cluster. This value will match the autoregistration action
33+
`CONFIG_FILE` - config file path. (Used if custom file and path needed)
1434

1535
## Usage
16-
### Basic
36+
### Basic
1737
docker run \
1838
-d \
1939
-p 10050:10050 \
@@ -24,25 +44,27 @@ Change it to match your server
2444
-d \
2545
-p 10050:10050 \
2646
-v /my-zabbix-agent-config.conf:/etc/zabbix_agentd.conf \
27-
--env="ZABBIX_SERVER=my.ip" \
28-
million12/zabbix-agent
47+
-e ZABBIX_SERVER=zabbix_server.ip \
48+
-e HOSTNAME=my.zabbix \
49+
-e CONFIG_FILE=/etc/zabbix_agentd.conf \
50+
million12/zabbix-agent
2951

30-
#### CoreOS
52+
#### Read data from Host OS
3153
docker run \
3254
-d \
3355
-p 10050:10050 \
34-
-v /proc:/proc \
35-
-v /sys:/sys \
36-
-v /dev:/dev \
56+
-v /proc:/data/proc \
57+
-v /sys:/data/sys \
58+
-v /dev:/data/dev \
3759
-v /var/run/docker.sock:/var/run/docker.sock \
3860
-v /my-zabbix-agent-config.conf:/etc/zabbix_agentd.conf \
39-
--env="ZABBIX_SERVER=my.ip" \
61+
--env="ZABBIX_SERVER=zabbix_server.ip" \
4062
million12/zabbix-agent
41-
42-
## Author
4363

44-
Author: Przemyslaw Ozgo (<linux@ozgo.info>)
64+
## Author
65+
Author: Przemyslaw Ozgo (<linux@ozgo.info>)
66+
This work is also inspired by [bhuisgen's](https://github.com/bhuisgen) work on his [docker images](https://github.com/bhuisgen/docker-zabbix-coreos). Many thanks!
4567

4668
---
4769

48-
**Sponsored by** [Typostrap.io - the new prototyping tool](http://typostrap.io/) for building highly-interactive prototypes of your website or web app. Built on top of TYPO3 Neos CMS and Zurb Foundation framework.
70+
**Sponsored by [Prototype Brewery](http://prototypebrewery.io/)** - the new prototyping tool for building highly-interactive prototypes of your website or web app. Built on top of [Neos CMS](https://www.neos.io/) and [Zurb Foundation](http://foundation.zurb.com/) framework.

circle.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
machine:
22
services:
33
- docker
4+
environment:
5+
ZABBIX_SERVER: my.server.com
6+
HOSTNAME: my.hostname.com
7+
HOST_METADATA: my.metadata.com
48

59
dependencies:
610
pre:
711
- docker pull centos:centos7
812
post:
9-
- docker build -t million12/zabbix-agent:test .
13+
- docker build -t million12/zabbix-agent .
1014

1115
test:
1216
override:
13-
- docker run -d --name zabbix-agent --net host million12/zabbix-agent:test
14-
- sleep 5
15-
- docker ps -a | grep "zabbix-agent" | grep Up
17+
- docker run -d --name zabbix-agent --net host -e ZABBIX_SERVER=$ZABBIX_SERVER -e HOSTNAME=$HOSTNAME -e HOST_METADATA=$HOST_METADATA million12/zabbix-agent
18+
- docker logs -f zabbix-agent | tee -a ${CIRCLE_ARTIFACTS}/zabbix-agent.log:
19+
background: true
20+
- while true; do if grep "Press Ctrl+C to exit" -a ${CIRCLE_ARTIFACTS}/zabbix-agent.log; then break; else sleep 1; fi done
21+
- grep $ZABBIX_SERVER -a ${CIRCLE_ARTIFACTS}/zabbix-agent.log
22+
- grep $HOSTNAME -a ${CIRCLE_ARTIFACTS}/zabbix-agent.log
23+
- grep $HOST_METADATA -a ${CIRCLE_ARTIFACTS}/zabbix-agent.log

container-files/bootstrap.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/sh
2+
set -eu
3+
export TERM=xterm
4+
# Agent ConfigFile
5+
CONFIG_FILE="/usr/local/etc/zabbix_agentd.conf"
6+
# Bash Colors
7+
red=`tput setaf 1`
8+
green=`tput setaf 2`
9+
yellow=`tput setaf 3`
10+
white=`tput setaf 7`
11+
bold=`tput bold`
12+
reset=`tput sgr0`
13+
separator=$(echo && printf '=%.0s' {1..100} && echo)
14+
# Logging Finctions
15+
log() {
16+
if [[ "$@" ]]; then echo "${bold}${green}[LOG `date +'%T'`]${reset} $@"; else echo; fi
17+
}
18+
### Magic Starts here
19+
update_config() {
20+
log "Updating configuration file..."
21+
if [[ "$ZABBIX_SERVER" != "127.0.0.1" ]]; then
22+
log "Changing Zabbix Server IP to ${bold}${white}${ZABBIX_SERVER}${reset}"
23+
sed -i 's/Server=127.0.0.1/Server='$ZABBIX_SERVER'/g' ${CONFIG_FILE}
24+
fi
25+
if [[ "$HOSTNAME" != "zabbix.agent" ]]; then
26+
log "Changing Zabbix Hostname to ${bold}${white}${HOSTNAME}${reset}."
27+
sed -i 's/Hostname=Zabbix server/Hostname='$HOSTNAME'/g' ${CONFIG_FILE}
28+
fi
29+
if [[ "$HOST_METADATA" != "zabbix.agent" ]]; then
30+
log "Changing Zabbix Host Metadata to ${bold}${white}${HOST_METADATA}${reset}."
31+
sed -i 's/# HostMetadata=/HostMetadata='$HOST_METADATA'/g' ${CONFIG_FILE}
32+
fi
33+
log "Config updated"
34+
}
35+
print_config() {
36+
log "Current Zabbix Agent config ${CONFIG_FILE}:"
37+
printf '=%.0s' {1..100} && echo
38+
cat ${CONFIG_FILE}
39+
echo ""
40+
printf '=%.0s' {1..100} && echo
41+
}
42+
start() {
43+
log "Starting Zabbix Agent using configuration file: ${CONFIG_FILE}"
44+
print_config
45+
zabbix_agentd -f -c ${CONFIG_FILE}
46+
}
47+
48+
if [[ $(grep "million12/zabbix-agent" ${CONFIG_FILE}) ]]; then
49+
log "Loading default config."
50+
update_config
51+
start
52+
else
53+
log "Loading custom config: ${CONFIG_FILE}"
54+
start
55+
fi
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
# start_agent() {
76+
# zabbix_agentd -f -c ${CONFIG_FILE}
77+
# }
78+
# if [[ $ZABBIX_SERVER != "127.0.0.1" ]]; then
79+
# log "Changing Zabbix Server IP to ${bold}${white}${ZABBIX_SERVER}${reset}."
80+
# sed -i 's/Server=127.0.0.1/Server='$ZABBIX_SERVER'/g' ${CONFIG_FILE}
81+
# fi
82+
# log "Startting agent..."
83+
# log `start_agent`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
3+
DOCKER_SOCKET=/var/run/docker.sock
4+
5+
CONFIG_FILE=/usr/local/etc/scripts/trapper/docker.conf
6+
7+
[ -r "$CONFIG_FILE" ] && . $CONFIG_FILE
8+
9+
CTS=$(echo "GET /containers/json?all=1 HTTP/1.0\r\n"|sudo netcat -U "$DOCKER_SOCKET"|tail -n +5)
10+
LEN=$(echo "$CTS"|jq 'length')
11+
12+
for I in $(seq 0 $((LEN-1)))
13+
do
14+
NAME=$(echo "$CTS"|jq ".[$I].Names[0]"|sed -e 's/^"\//"/')
15+
16+
DATA="$DATA,"'{"{#NAME}":'$NAME'}'
17+
done
18+
19+
echo '{"data":['${DATA#,}']}'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
DOCKER_SOCKET=/var/run/docker.sock
4+
5+
CONFIG_FILE=/usr/local/etc/scripts/trapper/docker.conf
6+
7+
[ -r "$CONFIG_FILE" ] && . $CONFIG_FILE
8+
9+
CTS=$(echo "GET /containers/json?all=1 HTTP/1.0\r\n"|sudo netcat -U "$DOCKER_SOCKET"|tail -n +5)
10+
LEN=$(echo "$CTS"|jq 'length')
11+
12+
for I in $(seq 0 $((LEN-1)))
13+
do
14+
ID=$(echo "$CTS"|jq ".[$I].Id"|sed -e 's/^"//' -e 's/"$//')
15+
NAME=$(echo "$CTS"|jq ".[$I].Names[0]"|sed -e 's/^"\//"/')
16+
CT=$(echo "GET /containers/$ID/json HTTP/1.0\r\n"|sudo netcat -U "$DOCKER_SOCKET"|tail -n +5)
17+
RUNNING=$(echo "$CT"|jq ".State.Running"|sed -e 's/^"//' -e 's/"$//')
18+
if [ "$RUNNING" = "true" ]; then
19+
TOP=$(echo "GET /containers/$ID/top?ps_args=-aux HTTP/1.0\r\n"|sudo netcat -U "$DOCKER_SOCKET"|tail -n +5)
20+
PS=$(echo "$TOP"|jq ".Processes")
21+
PS_LEN=$(echo "$PS"|jq "length")
22+
23+
for J in $(seq 0 $((PS_LEN-1)))
24+
do
25+
P=$(echo "$PS"|jq ".[$J]")
26+
27+
PID=$(echo "$P"|jq ".[1]"|sed -e 's/^"//' -e 's/"$//')
28+
CMD=$(basename $(echo "$P"|jq ".[10]"|sed -e 's/^"//' -e 's/"$//'|cut -d' ' -f1))
29+
30+
DATA="$DATA,"'{"{#NAME}":'$NAME',"{#PID}":'$PID',"{#CMD}":"'$CMD'"}'
31+
done
32+
fi
33+
done
34+
35+
echo '{"data":['${DATA#,}']}'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DOCKER_SOCKET=/var/run/docker.sock

0 commit comments

Comments
 (0)