Zabbix Plan Downtime

Prawit Tangmanopeanchai
4 min readNov 16, 2022

--

16 November 2022

ปกติเวลาเราจะทำการ maintenance เครื่องแล้วไม่ต้องการให้ zabbix แจ้ง alert ออกมาก็จะไปสร้าง maintenance period โดยไปที่ menu Configuration > Maintenance

จากนั้นก็ทำการสร้าง maintenance period โดยกดที่ปุ่ม “Create maintenance period”

จากนั้นก็ใส่ข้อมูลตามหน้าจอให้ครบ

ถ้าทำได้ตามนี้ก็คงไม่มีปัญหาอะไร

แต่วันนี้พอดีอยากจะสร้าง maintenance period แบบ command line เพื่อไปใช้กับ script ที่ใช้สำหรับทำการ reboot เครื่อง ปกติ script นี้จะตั้งเวลาในการทำงานตามที่กำหนดไว้ เพื่อที่จะไม่ให้เกิด alert ขึ้นจาก zabbix ในขณะที่เครื่องกำลัง reboot อยู่จึงต้องทำการกำหนด maintenance period เอาไว้ก่อนที่จะทำการ reboot โดยเราจะทำการเพิ่ม script ในการสร้าง maintenance period นี้เอาไว้ก่อนคำสั่ง reboot

ก่อนที่เราจะใช้ command line ของ zabbix ได้เราจะต้องทำการติดตั้ง zabbix-cli เสียก่อน โดยใช้คำสั่ง

pip install git+https://github.com/unioslo/zabbix-cli.git@master

หลังติดตั้งเสร็จแล้วให้ทำการกำหนด url ของ zabbix server ของเราโดยใช้คำสั่ง

zabbix-cli-init --zabbix-url https://<zabbix server>/

จากนั้นเรามาลองดูว่าสามารถใช้งานคำสั่งอะไรได้บ้างโดยใช้คำสั่ง

zabbix-cli -C "help"

จะได้ output ดังนี้

เรามาลองใช้คำสั่ง “show_hosts” เพื่อดูข้อมูล host ของเรา ตอนนี้ที่ zabbix server มี host อยู่เพียงแค่ตัวเดียว ชื่อ “Zabbix_server”

ตัวอย่างคำสั่งในการแสดงข้อมูลของ host เป็นดังนี้

zabbix-cli -C "show_host Zabbix_server"

จะได้ output ดังนี้

จะเห็นว่า zabbix-cli จะถาม user/password ของ zabbix ทุกครั้งถึงจะทำงานต่อได้ หากไม่ต้องการให้ zabbix-cli มีการถาม user/password ทุกครั้งให้ทำการใส่ user/password เอาไว้ใน file “~/.zabbix-cli_auth” โดยมี format ดังนี้ “USERNAME::PASSWORD” (สามารถดูตัวอย่างได้จาก link อ้างอิงด้านล่าง)

ตัวอย่างการใช้คำสั่งต่าง ๆ สามารถดูได้จากคำสั่ง help แล้วตามด้วยคำสั่งที่ต้องการ เช่น หากเราต้องการดูว่าจะใช้คำสั่ง zabbix-cli เพื่อ show_items จะต้องใส่อย่างไร ก็พิมพ์คำสั่งตามนี้

root@zabbix-server:~# zabbix-cli -C "help show_items"

DESCRIPTION:
This command shows items that belong to a template

COMMAND:
show_items [template]

[template]
----------
Template name or zabbix-templateID

คราวนี้เราก็พร้อมที่จะสร้าง maintenance period ด้วย zabbix-cli แล้ว

ก่อนอื่นเราก็มาดูกันก่อนว่าคำสั่งนี้ใช้อย่างไร

root@zabbix-server:~# zabbix-cli -C "help create_maintenance_definition"

DESCRIPTION:

This command creates a 'one time only' maintenance definition
for a defined period of time. Use the zabbix dashboard for
more advance definitions.

COMMAND:
create_maintenance_definition [name]
[description]
[host/hostgroup]
[time period]
[maintenance type]

[name]
------
Maintenance definition name.

[description]
-------------
Maintenance definition description

[host/hostgroup]
----------------
Host/s and/or hostgroup/s the that will undergo
maintenance.

One can define more than one value in a comma separated list
and mix host and hostgroup values.

[time period]
-------------
Time period when the maintenance must come into effect.

One can define an interval between two timestamps in ISO format
or a time period in minutes, hours or days from the moment the
definition is created. ::

2016-11-21T22:00 to 2016-11-21T23:00

Means 22:00 until 23:00 on 2016-11-21. ::

2 hours

Means 2 hours from the moment we create the maintenance.

[maintenance type]
------------------
Maintenance type.

คำสั่งนี้ต้องการ parameter ทั้งหมด 5 ตัว คือ

name : ชื่อของ maintenance period
description : คำอธิบาย
host/hostgroup : ชื่อของ host หรือ hostgroup ที่ต้องการกำหนด maintenance period
time period : ช่วงเวลาของการทำ maintenance โดยวันและเวลาจะเป็น GMT เช่นถ้าเราใส่เวลาเป็น 2022–11–21T22:00 เวลาที่จะมีผลจริงจะเป็น 2022–11–22 05:00 ในเวลาบ้านเรา
maintenance type : บอกว่าช่วง maintenance period นั้นจะให้ zabbix ยังเก็บข้อมูลต่อไปหรือไม่

ตัวอย่างนี้เราจะทำการกำหนด maintenance period ชื่อ “Plan downtime” ให้กับ hostgroup “Zabbix servers” ในวันที่ 22 November 2022 เวลา 05:00 ถึง 06:00 โดยไม่ได้ใส่ description และไม่เก็บข้อมูลในช่วง maintenance period

คำสั่งที่ใช้จะเป็นดังนี้

zabbix-cli -C "create_maintenance_definition 'Plan downtime' '' 'Zabbix servers' '2022-11-21T22:00 to 2022-11-21T23:00' '1'"

หลังจากเราสั่งไปแล้วเมื่อเข้าไปดูใน zabbix server เราจะ Plan downtime ในหน้า maintenance

เท่านี้เราก็สร้าง maintenance period ด้วย command line เพื่อนำไปใช้ใน script ได้แล้ว

เพิ่มเติม : ชื่อของ maintenance period จะไม่สามารถซ้ำกันได้ ดังนั้นหากเราต้องการใช้ชื่อของ maintenance period ชื่อเดิม เราจะต้องลบ maintenance period เดิมออกก่อนจึงจะสามารถสร้างใหม่ได้ (แต่ไม่แนะนำให้ลบนะครับ แนะนำให้สร้างใหม่ดีกว่า เราจะได้ทราบว่ามีการทำ maintenance อะไรไปบ้าง

การลบ maintenance period สามารถลบได้จากหน้า gui ของ zabbix server ได้เลย แต่หากต้องการจะลบโดยใช้ command line จะสามารถลบได้โดยใช้คำสั่ง “remove_maintenance_definition”

root@zabbix-server:~# zabbix-cli -C "help remove_maintenance_definition"

DESCRIPTION:
This command removes one or several maintenance definitions

COMMAND:
remove_maintenance_definitions [definitionID]

[definitionID]
--------------
Definition ID. One can define more than one value in a comma
separated list.

ซึ่งจะเห็นว่าการจะลบได้จะต้องทราบค่าของ definitionID ซึ่งค่าของ definitionID นี้เราจะไม่เห็นจากหน้า gui จะต้องใช้คำสั่ง “show_maintenance_definitions” ถึงจะเห็น

root@zabbix-server:~# zabbix-cli -C "show_maintenance_definitions * * *"
+----+---------------+----------------+--------+----------------------+-----------+----------------+-------------+
| ID | Name | Type | State | To | Hostnames | Hostgroups | Description |
+----+---------------+----------------+--------+----------------------+-----------+----------------+-------------+
| 2 | Plan downtime | Without DC (1) | Active | 2022-11-21T23:00:00Z | | Zabbix servers | |
+----+---------------+----------------+--------+----------------------+-----------+----------------+-------------+

จากตัวอย่างด้านบน จะเป็นค่าของ definitionID คือ 2

ครับ ก็หวังว่าจะเป็นประโยชน์กับทุกท่านครับ

อ้างอิง : https://github.com/unioslo/zabbix-cli

--

--

Prawit Tangmanopeanchai
Prawit Tangmanopeanchai

Written by Prawit Tangmanopeanchai

Head of Cloud Implementation Unit at gosoft (Thailand)

No responses yet