[TOC]
# zabbix server
## Pre-Requsities
:::info
We assume you have a running Ubuntu 20.04 system with sudo privileged account access. Follow the below steps to install the Zabbix server on your Ubuntu system.
:::
:::success
### Step 1 – Setup LAMP
- Zabbix required PHP programming language to run, MySQL as a database server, and a Web server like Apache or Nginx. We use an Apache web server for this tutorial. Let’s have the installation of all the required packages on your system by running the following commands.
```=
sudo apt update
sudo apt install apache2 libapache2-mod-php
sudo apt install php php-mbstring php-gd php-xml php-bcmath php-ldap php-mysql
```
install mysql-server : follow this [Source](https://hackmd.io/0Cg5DF3oSyGy7xyc9XBrgA)
- You update the PHP configuration variables. Edit the PHP configuration file /etc/php/7.4/apache2/php.ini for Apache and update the timezone as per your requirements.
```
vim /etc/php/7.4/apache2/php.ini
```
```
memory_limit 256M
upload_max_filesize 16M
post_max_size 16M
max_execution_time 300
max_input_time 300
max_input_vars 10000
date.timezone = 'Asia/Kolkata'
```
<font color="#f00">you need to follow your zabbix.conf path.</font> if not you will have some problem. look problem 1.
### Step 2 – Configure Zabbix Repository
- Zabbix’s official team provides Apt package repositories for the Debian-based system. Use the following commands to add the repository to your Ubuntu system.
```=
wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+focal_all.deb
sudo dpkg -i zabbix-release_5.0-1+focal_all.deb
```
### Step 3 – Installing Zabbix Server
- Once you added the Apt repository, use the following commands to install Zabbix server packages. Here zabbix-server-mysql package includes the Zabbix server with MySQL support. The zabbix-frontend-php package provides the web interface for the Zabbix server.
```=
sudo apt update
sudo apt install zabbix-server-mysql zabbix-frontend-php zabbix-agent zabbix-apache-conf
```
### Step 4 – Create Zabbix Database and User
- Next, create a database schema for the Zabbix server. Login to the MySQL server with the root account and create MySQL database and user with the following commands.
```
mysql -u root -p
mysql>CREATE DATABASE zabbixdb character set utf8 collate utf8_bin;
mysql>CREATE USER 'zabbix'@'localhost' IDENTIFIED BY 'password';
mysql>GRANT ALL PRIVILEGES ON zabbixdb.* TO 'zabbix'@'localhost' WITH GRANT OPTION;
mysql>FLUSH PRIVILEGES;
```
- After creating the database, load the default schema of Zabbix of the database.
```=
cd /usr/share/doc/zabbix-server-mysql
zcat create.sql.gz | mysql -u zabbix -p zabbixdb
```
### Step 5 – Update Zabbix Configuration
- Edit Zabbix server configuration file /etc/zabbix/zabbix_server.conf in your favorite text editor and update the following database configurations. This will be used by the Zabbix server to connect to the database
```=
sudo vi /etc/zabbix/zabbix_server.conf
```
```
DBHost=localhost
DBName=zabbixdb
DBUser=zabbix
DBPassword=password
```
- Now, enable the Zabbix service to start on system boot and restart the service to reload new settings.
```=
sudo systemctl enable zabbix-server
sudo systemctl restart zabbix-server
```
- The Zabbix package also creates its own Apache configuration file ie /etc/zabbix/apache.conf and makes a link to the Apache configuration directory. Let’s use the following command to restart the Apache service
```=
sudo systemctl restart apache2
```
- Now your system is ready for the Zabbix installation. Just go to the Zabbix web installer and finish the installation.
### Step 6 – Adjust Firewall for Zabbix
- Next, you will need to allow the Zabbix ports 10050 and 10051. and HTTP service through firewalld. You can allow them with the following command:
```=
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-port=10050/tcp
sudo firewall-cmd --permanent --add-port=10051/tcp
sudo firewall-cmd --reload
```
if meet the problem, look problem2.
### Step 7 – Running Zabbix Web Installer
`192.168.1.215/zabbix/`



```
password : password //this is deafault
```

```
Username: Admin
Password: zabbix
```


:::
---
# zabbix agent
:::success
### Step1 : Import Zabbix installation software into Ubuntu software package management tool。
- <font color="#f00">Please remember to check the version before executing the action.</font>
```=
wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+focal_all.deb
sudo dpkg -i zabbix-release_5.0-1+focal_all.deb
sudo apt update
```
### Step2 : install Zabbix Agent
```=
sudo apt install zabbix-agent
```
### Step3 : Parameter settings
```=
vim /etc/zabbix/zabbix_agentd.conf
```

把Server改成Server IP.

改成 機器名稱,等需要與網頁上新增主機的 Host name 一致,且不能重複。
Hostname=kevin server
### Step4 : Set to start automatically at boot
```=
systemctl restart zabbix-agent
systemctl enable zabbix-agent
```
### Step5 : Add a new host on the Server
Zabbix web page > Configuration > Hosts > Create host

'*' means required
Host name: 215Server (same as Hostname in zabbix_agentd.conf)
Groups: Linux servers
Interfaces IP address: IP where Agent is installed
### Step6 : Switch Templates tab

### Step7 : Finally click Add to complete

Wait for a while for ZBX to turn green and it will be considered complete.
:::
---
## problem
:::danger
- p1
Time zone for PHP is not set (configuration parameter "date.timezone").

you must find your correct file.
- p2

need to :
`sudo apt install firewalld`
and :
```=
$ sudo systemctl start firewalld
$ sudo systemctl enable firewalld
$ sudo systemctl status firewalld
```

- p3


session_start() open failed Permission denied (13)错误
```=
sudo chmod 777 php //更改php權限
sudo systemctl restart apache2 //重新啟動
```

指令沒安裝好
```
zcat create.sql.gz | mysql -u zabbix -p zabbixdb
```
- p4
server不知道為甚麼沒啟動

查看log檢查問題


Zabbix 服务器无法连接到数据库 'zabbixdb',原因是访问用户 'zabbix' 无法通过提供的密码获得访问权限。
進入mysql root帳戶,看是否有創建成功zabbix

進入文件

加入`skip-grant-tables`指令 //不用密碼即可進入DB,有時候DB密碼打對還是無法進入。

:::
---
create the user zabbix and password is rtlab666.

192.168.1.214

确保 10051 端口没有被其他进程占用。您可以使用 netstat 或 ss 命令来检查端口的占用情况。
`netstat -tuln | grep 10051`
---
获取每个分区的磁盘使用率 :`df -h`

---
# Zabbix 架構流程
:::success

1. 建立Host時會要求選擇Group,可以有多個Group,分組可以用來做告警發給特定人士或儀表板等用途。
2. 接著Link templates,templates可以增加監控項目 EX.ping,加速監控設定,當然也可以不用templates,直接在host建立專屬的items。
3. Triger監控items收集到的data,運算後觸發規則,轉換狀態從正常到告警
4. Action可以設定某個group或host,狀態變成告警,發送訊息給那些人
:::
# API
:::success
開頭:`curl -s -X POST -H 'Content-Type:application/json'`
結尾: `http://192.168.1.215/zabbix/api_jsonrpc.php | python3 -m json.tool`
- step1 :
輸入 :
```
curl -s -X POST -H 'Content-Type:application/json' -d '{"jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix"}, "id": 1}' http://192.168.1.215/zabbix/api_jsonrpc.php | python3 -m json.tool
```
獲取Auth Token
- step2 :
**Log**

:::
---
# 參考資料
zabbix agent
https://ithelp.ithome.com.tw/m/articles/10267006
zabbix 介紹
https://www.cnblogs.com/qingbaizhinian/p/13700957.html#_label2
zabbix 手冊
https://www.zabbix.com/documentation/current/zh/manual/config/items/itemtypes/http