Zabbix - cgi and mysql

Zabbix is the most popular monitoring manager in open source world, there's monitoring itself only like catci, but the hint is that Zabbix is manager also, is ipv6 ready! fully supported, also featured proxy-server for hidden and firewalled networks. Its like nagios one but more big, cutting edge and professional.
In the wiki there are two approaches for its use, the professional one (for servers and deploys) and the fast and simple usage (for developers and/or enthusiasts). For more curses on that (of course focused on alpine) check Complete alpine documentation on english for zabbix curse
This documentation is divided in two big parts.. the prerequisites and the installation of zabbix minimal infrastructure. The first part are in sync with the current alpine wiki pages of Production pages
Note: There is a possibility that this page may have been complexified / obfuscated. See the talk page or review an earlier edit: https://wiki.alpinelinux.org/w/index.php?title=Zabbix&oldid=17984.
= Where to use zabbix and alpine =
Since Zabbix it focuses on hosts: So is the right choice for monitoring distributed networks (was originally developed for monitoring servers).
In cases where there is no option to install an agent, Zabbix offers basic agentless monitoring. With it, you can check the availability of network services, as well as execute remote commands
== Zabbix infraestructure ==
Zabbix is divided into two large parts: server(s) and agent(s), and yes "servers" cos can be centralized or decentralized due the "proxyes" semi-servers.
* The servers: represent all the recollected data and has management capabilities. Each one if are more than one, collects and stores statistical data.
* The agents: represent each host, the clients, those machines or devices from which data is collected. Those support both passive (polling) and active checks (trapping).
* The proxys: represent a server that can act as man-in-the-middle for distributed load, cases of hideden networking or firewalled, of course with management capabilities cos act as server for those agents/hosts.
Passive checks mean that the Zabbix server requests a value from the Zabbix agent, and the agent processes the request and returns the value to the Zabbix server.
Active checks mean that the Zabbix agent requests a list of active checks from the Zabbix server and then periodically sends the results.
= Part 1 Pre-requisites: php, webserver and database =
= Part 2: zabbix =
Zabbix depends on which type of database you will use, here we will show you how to doit with mysql, as more easy to use, for production is recommended posrgresql as in 
== 1. install zabbix packages ==
Remenber that Zabbix has 3 components, and the server is the main one
* Central servers to represent the data ( can be one zabbix server, o combined zabbix server with more zabbix proxies)
* Clients hosts to monitored (that is made by Agents, SNMP or just ICMP)
* Distributed proxy servers (that is only need for hosts behind firewall or closed networks)
In the server we need the central base service at leas, and of course one agent for monitoring that server:
<nowiki>
cat > /etc/apk/repositories << EOF
https://dl-4.alpinelinux.org/alpine/v$(cat /etc/alpine-release | cut -d'.' -f1,2)/main
https://dl-4.alpinelinux.org/alpine/v$(cat /etc/alpine-release | cut -d'.' -f1,2)/community
EOF
apk update
apk add zabbix zabbix-mysql zabbix-webif zabbix-setup zabbix-utils zabbix-agentd
</nowiki>
Now lest configure the server and the UI frontend in that server:
== 2. Configure the base service ==
Server zabbix, or proxy one always need a main database, the service of monitoring is running with it, the UI and agents always send the data to the zabbix server or zabbix proxy (and from proxy to server).
<nowiki>
mysql -u root -e "CREATE USER 'zabbix'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('clavezabix');"
mysql -u root -e "CREATE DATABASE zabbix CHARACTER SET utf8 COLLATE utf8_bin;"
mysql -u root -e "GRANT ALL PRIVILEGES ON zabbixdb.* TO 'zabbixdb'@'localhost' WITH GRANT OPTION;"
mysql -u zabbixdb --password=clavezabix zabbixdb < /usr/share/zabbix/database/mysql/schema.sql
mysql -u zabbixdb --password=clavezabix zabbixdb < /usr/share/zabbix/database/mysql/images.sql
mysql -u zabbixdb --password=clavezabix zabbixdb < /usr/share/zabbix/database/mysql/data.sql
sed -i 's|.*DBHost.*=.*|DBHost=localhost|g' /etc/zabbix/zabbix_server.conf
sed -i 's|DBName.*=.*|DBName=zabbixdb|g' /etc/zabbix/zabbix_server.conf
sed -i 's|.*DBPassword.*=.*|DBPassword=zabbixdb.db.1.com.1|g' /etc/zabbix/zabbix_server.conf
sed -i 's|.*DBUser.*=.*|DBUser=zabbixdb|g' /etc/zabbix/zabbix_server.conf
sed -i 's|.*DBSocket.*=.*|DBSocket=/run/mysqld/mysqld.sock|g' /etc/zabbix/zabbix_server.conf
sed -i 's|.*Fping6Location.*=.*|Fping6Location=|g' /etc/zabbix/zabbix_server.conf
sed -i 's|.*FpingLocation.*=.*|FpingLocation=/usr/sbin/fping|g' /etc/zabbix/zabbix_server.conf
sed -i 's|Nostname=.*|Nostname=monitor.zabbixnetwork|g' /etc/zabbix/zabbix_server.conf
sed -i 's|.*SourceIP.*=.*|SourceIP=0.0.0.0|g' /etc/zabbix/zabbix_server.conf
chown root:zabbix /usr/sbin/fping
rc-update add zabbix-server default
rc-service zabbix-server restart
</nowiki>
== 3. Setup and configure web frontend ==
The main server can be managed with the zabbix-frontend:
<nowiki>
cp /usr/share/webapps/zabbix/conf/zabbix.conf.php.example /usr/share/webapps/zabbix/conf/zabbix.conf.php
sed -i "s|.*DB\['TYPE'\].*=.*|\$DB\['TYPE'\] = 'MYSQL';|g" /usr/share/webapps/zabbix/conf/zabbix.conf.php
sed -i "s|.*DB\['SERVER'\].*=.*|\$DB\['SERVER'\] = 'localhost';|g" /usr/share/webapps/zabbix/conf/zabbix.conf.php
sed -i "s|.*DB\['DATABASE'\].*=.*|\$DB\['DATABASE'\] = 'zabbixdb';|g" /usr/share/webapps/zabbix/conf/zabbix.conf.php
sed -i "s|.*DB\['USER'\].*=.*|\$DB\['USER'\] = 'zabbixdb';|g" /usr/share/webapps/zabbix/conf/zabbix.conf.php
sed -i "s|.*DB\['PASSWORD'\].*=.*|\$DB\['PASSWORD'\] = 'zabbixdb.db.1.com.1';|g" /usr/share/webapps/zabbix/conf/zabbix.conf.php
sed -i "s|.*ZBX_SERVER_PORT.*=.*|\$ZBX_SERVER_PORT = '10051';|g" /usr/share/webapps/zabbix/conf/zabbix.conf.php
sed -i "s|.*ZBX_SERVER_NAME.*=.*|\$ZBX_SERVER_NAME = 'monitor.ruices';|g" /usr/share/webapps/zabbix/conf/zabbix.conf.php
sed -i "s|.*IMAGE_FORMAT_DEFAULT.*=.*|\$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_GIF;|g" /usr/share/webapps/zabbix/conf/zabbix.conf.php
sed -i "s|.*ZBX_SERVER.*=.*'localhost';|\$ZBX_SERVER = 'localhost';|g" /usr/share/webapps/zabbix/conf/zabbix.conf.php
cat > /usr/share/webapps/zabbix/.user.ini << EOF
date.timezone = "America/Caracas"
display_errors = Off
log_errors = On
upload_max_filesize = 16M
post_max_size = 24M
memory_limit = 512M
register_globals = Off
magic_quotes_gpc = Off
magic_quotes_runtime = Off
session.auto_start = 0
mbstring.func_overload = 0
max_execution_time = 600
max_input_time = 600
EOF
</nowiki>
We do not use the stupid link of direct put on htdocs, we are professional and we use aliasing, 
now lest configure the webserver to server the frontend by web:
<nowiki>
cat > /etc/lighttpd/mod_zabbix.conf << EOF
alias.url += (
 "/zabbix/" => "/usr/share/webapps/zabbix/"
)
\$HTTP["url"] =~ "^/zabbix/" {
 dir-listing.activate = "disable"
}
EOF
sed -i 's#\#.*mod_rewrite.*,.*# "mod_rewrite",#g' /etc/lighttpd/lighttpd.conf
sed -i 's#\#.*mod_alias.*,.*# "mod_alias",#g' /etc/lighttpd/lighttpd.conf
sed -i 's#\#.*mod_accesslog.*,.*# "mod_accesslog",#g' /etc/lighttpd/lighttpd.conf
sed -i 's#\#.*mod_setenv.*,.*# "mod_setenv",#g' /etc/lighttpd/lighttpd.conf
sed -i 's#\#.*mod_redirect.*,.*# "mod_redirect",#g' /etc/lighttpd/lighttpd.conf
sed -i 's#.*include "mod_cgi.conf".*# include "mod_cgi.conf"#g' /etc/lighttpd/lighttpd.conf
check="";check=$(grep 'include "mod_zabbix.conf' /etc/lighttpd/lighttpd.conf);[[ "$check" != "" ]] && echo listo || sed -i 's#.*include "mod_fastcgi_fpm.conf".*#include "mod_fastcgi_fpm.conf"\ninclude "mod_zabbix.conf"#g' /etc/lighttpd/lighttpd.conf
chown -R lighttpd:www-data /usr/share/webapps/zabbix/
rc-service lighttpd restart
</nowiki>
You should now be able to browse to the Zabbix frontend: http://yourservername/zabbix/ or in http://localhost/zabbix.
The login using: Login name: Admin Password:zabbix. (as described at https://www.zabbix.com/documentation/4.0/manual/installation)
There's no need to web setup cos we made all in cli as best hackers admins.
== Zabbix tools for items maps or scripts ==
There's no complete features in busybox so need more tools, also, Zabbix requires special permissions to use tools
<nowiki>
apk add bash fping tcptraceroute coreutils net-snmp-tools nmap perl rrdtool font-dejavu net-snmp-libs
chown root:zabbix /usr/sbin/fping
chmod u+s /usr/bin/ping
chmod u+s /bin/ping
chmod u+s /usr/bin/nmap 
</nowiki>
= Zabbix and the Monitored host =
Zabbix can monitor almost any device system, including Alpine Linux hosts, home computers, celphones, network devices, etc.
== types of monitoring ==
The zabbix agent is a zabbix service that runs on computers and is fully feartured, the SNMP(Simple Network Manager Protocol) is a common standard protocol and is the second most supported, the ICMP(Internet Control Message Protocol) is the most used and simples way but most limited, check the features:
Feature 	By Agent 	By SNMP 	By ICMP
Ping to check alive (direct ip) 	Yes 	Yes but with zabbbix proxy if firewall	Only on direct ip, no firewall
Retreive data from programs (direct ping)	Yes 	Yes but with zabbbix proxy if firewall	No
Send commands to manage 	Yes 	Yes but using zabbix proxy if firewall	No
Runs on any device 	No, only computers 	Mostly any network device 	Yes
Runs on any OS 	YEs, limited in androita and mainframes	Yes 	Yes
Requires provilegies 	Not necesary for most fearures 	It depends 	No
Ensure that the readproc group exists (support added since alpine-baselayout-2.0_rc1-r1), by adding the following line to /etc/group:
== Monitoring by Zabbix Agents ==
The agent way to monitoring is the most complete featured, but only applies to computers host, becouse the zabbiz-agent software is only available for those devices, for networking devices you must use ICMP or SNMP (check next subsection).
So, install the agent package and configure it on each host to be monitored/managed (on server too):
<nowiki>
cat > /etc/apk/repositories << EOF
https://dl-4.alpinelinux.org/alpine/v$(cat /etc/alpine-release | cut -d'.' -f1,2)/main
https://dl-4.alpinelinux.org/alpine/v$(cat /etc/alpine-release | cut -d'.' -f1,2)/community
EOF
apk update
apk add zabbix-agent net-snmp-libs net-snmp-agent-libs
sed -i 's|.*ListenPort.*=.*|ListenPort=10050|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|.*Hostname=.*|Hostname=monitordhost1|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|Server =.*|Server=localhost,monitor.zabbixnetwork|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|ServerActive.*=.*|ServerActive=localhost,monitor.zabbixnetwork |g' /etc/zabbix/zabbix_agentd.conf|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|.*EnableRemoteCommands.*=.*|EnableRemoteCommands=1|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|.*LogRemoteCommands.*=.*|LogRemoteCommands=1|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|# Include=.*|Include=/etc/zabbix/zabbix_agentd.d/*.conf|' /etc/zabbix/zabbix_agentd.conf
rc-update add zabbix-agentd default
rc-service zabbix-agentd restart
</nowiki>
== Monitor by SNMP protocol ==
Then in each monitor system, you shoul define a community word due ISP's always will punish if you used public and open SNMP to world, inclusivelly you shoul protect inside your private network too, so on each host to be monitored you shoul made those commands to configure SNMP (including the zabbix server) using the :
<nowiki>
apk add net-snmp net-snmp-perl net-snmp-tools
cat > /etc/snmp/snmpd.conf << EOF
view systemonly included .1.3.6.1.2.1.1
view systemonly included .1.3.6.1.2.1.25.1
rocommunity monitor
rocommunity public localhost
rocommunity public default -V systemonly
sysLocation client host at monitor.zabbixnetwork
sysContact infoadmin <venenux@venenux.net>
sysServices 72
EOF
rc-update add snmpd default
rc-service snmpd restart 
</nowiki>
This will allow to any host consult SNMP v2c with word "monitor", but is best you use if have distributed network a SNMP v3 autentiated. 
== DB monitoring ==
Due limitations any host server that will have and database, must have and agent aither have behind or not have direct zabbix server/proxy connection.
In this case we used Mysql as example
=== on the monitored side: DBMS server to monitor ===
<nowiki>
apk add zabbix-agent
sed -i 's|.*ListenPort.*=.*|ListenPort=10050|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|.*Hostname=.*|Hostname=theDBserver|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|Server =.*|Server=monitor.zabbixnetwork|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|ServerActive.*=.*|ServerActive=monitor.zabbixnetwork|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|.*EnableRemoteCommands.*=.*|EnableRemoteCommands=1|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|.*LogRemoteCommands.*=.*|LogRemoteCommands=1|g' /etc/zabbix/zabbix_agentd.conf
sed -i 's|# Include=|Include=/etc/zabbix/zabbix_agentd.d/*.conf|g' /etc/zabbix/zabbix_agentd.conf
rc-update add zabbix-agentd default
rc-service zabbix-agentd restart
apk add mariadb-client 
wget -O /etc/zabbix/zabbix-agent/userparameter_mysql.conf "https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/conf/zabbix_agentd/userparameter_mysql.conf?at=release/5.0"
mysql -u root -e "CREATE USER 'zabbix'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('zabbix.db.1.com.1');"
mysql -u root -e "GRANT USAGE,REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zabbix'@'localhost';"
</nowiki>
=== on the zabbix server side: to configure the monitored host ===
Use the template DB mysql o DB postgresql on the configured host and remenber that hostname in the configured host must be the same in the hostname configured in the agent configuration file of the monitored host.
For most details check the next section crash course:
= Optional: Crash course in adding hosts, checks, and notifications =
Note: This is optional since it's not specific to Alpine Linux, but I wanted a couple notes for how to perform a simple check on a server that doesn't have the agent installed on it, and be notified on state changes.
Administration -> Media Types -> Email
* Setup server, helo, email from address
Administration -> Users
* Setup each user who'll get notified, make sure they have media type "Email" added with their address
Configuration -> Hosts -> Create host
* In Linux Servers hostgroup
* Define dns name, ip, connect by IP
* If the machine is a simple networking device that will only be monitored using SNMP, add it to Template_SNMPv2_Device, and you're done.
Configuration -> Templates -> Create template
* Give it a name (Template_Alpine_Linux_Infra_HTTP)
* In Templates group
Configuration -> Templates -> Template_Alpine_Linux_Infra_HTTP -> Items
* Create Item
* Host: Template_Alpine_Linux_Infra_HTTP
* Description: HTTP Basic Check
* Type: Simple_check
* Key: http,80
Configuration -> Templates -> Template_Alpine_Linux_Infra_HTTP -> Triggers
* Create Trigger
* Name: "HTTP Trigger"
* Expression: {Template_Alpine_Linux_Infra_HTTP:http,80.last(0)}#1
* Severity: High
Configuration -> Actions -> 
* Create Action
* name: Email notifications
* Event source: triggers
* Default Subject: add "{HOST.DNS}:" to the beginning
* Default message: add "{HOST.DNS}:" to the beginning
* Conditions: make host have to be from "Linux Servers" hostgroup, and Template_Alpine_Linux_Infra_HTTP:HTTP trigger" is not 1
* Email affected users
== See also ==
* Cacti:_traffic_analysis_and_monitoring_network
== External resources and complete guides ==
* Complete alpine documentation on englilsh for zabbix curse
* Complete alpine documentation on spanish for zabbix curse
Category:Monitoring
Category:PHP
Category:SQL