Cách cài đặt công cụ giám sát Nagios trên Ubuntu 22.04

theanh

Administrator
Nhân viên
Nagios là một công cụ giám sát mã nguồn mở, miễn phí mạnh mẽ được sử dụng để giám sát máy chủ, mạng và cơ sở hạ tầng Linux và Windows. Với Nagios, bạn có thể giám sát mức sử dụng CPU, mức sử dụng đĩa và một số dịch vụ bao gồm HTTP, SSH, FTP, SMTP, v.v. Nó có giao diện web đơn giản và thân thiện với người dùng giúp bạn giám sát tất cả các máy chủ từ vị trí trung tâm. Nó có thể dễ dàng tích hợp với các ứng dụng của bên thứ ba với nhiều API. Nagios hoạt động bằng cách liên tục giám sát các máy chủ và dịch vụ mà bạn chỉ định và cảnh báo bạn khi có sự cố.

Hướng dẫn này sẽ chỉ cho bạn cách cài đặt Nagios Core trên máy chủ Ubuntu 22.04.

Điều kiện tiên quyết​

  • Máy chủ chạy Ubuntu 22.04.
  • Mật khẩu gốc được cấu hình trên máy chủ.

Cài đặt phụ thuộc bắt buộc​

Trước khi bắt đầu, bạn sẽ cần cập nhật gói hệ thống và cài đặt các phụ thuộc bắt buộc khác trên máy chủ của mình:
Mã:
apt-get update -y
apt-get install wget unzip vim curl openssl build-essential libgd-dev libssl-dev libapache2-mod-php php-gd php apache2 -y
Sau khi tất cả các gói được cài đặt, bạn có thể tiến hành bước tiếp theo.

Cài đặt Nagios Core từ Source​

Theo mặc định, phiên bản Nagios Core mới nhất không có trong kho lưu trữ mặc định 22.04. Vì vậy, nên biên dịch Nagios từ nguồn.

Đầu tiên, tải xuống phiên bản Nagios Core mới nhất từ kho lưu trữ Git bằng lệnh sau:
Mã:
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.7.tar.gz
Sau khi tải xuống, hãy giải nén tệp đã tải xuống bằng lệnh sau:
Mã:
tar -xvzf nagios-4.4.7.tar.gz
Tiếp theo, thay đổi thư mục thành thư mục đã giải nén và định cấu hình Nagios Core bằng lệnh sau lệnh:
Mã:
cd nagios-4.4.7
./configure --with-httpd-conf=/etc/apache2/sites-enabled
Bạn sẽ nhận được kết quả sau:
Mã:
*** Configuration summary for nagios 4.4.7 2022-04-14 ***: General Options: ------------------------- Nagios executable: nagios Nagios user/group: nagios,nagios Command user/group: nagios,nagios Event Broker: yes Install ${prefix}: /usr/local/nagios Install ${includedir}: /usr/local/nagios/include/nagios Lock file: /run/nagios.lock Check result directory: /usr/local/nagios/var/spool/checkresults Init directory: /lib/systemd/system Apache conf.d directory: /etc/apache2/sites-enabled Mail program: /bin/mail Host OS: linux-gnu IOBroker Method: epoll Web Interface Options: ------------------------ HTML URL: http://localhost/nagios/ CGI URL: http://localhost/nagios/cgi-bin/ Traceroute (used by WAP):
Review the options above for accuracy. If they look okay,
type 'make all' to compile the main program and CGIs.
Tiếp theo, chạy lệnh sau để biên dịch Nagios:
Mã:
make all
Tiếp theo, tạo người dùng và nhóm cần thiết, và thêm người dùng Nagios vào nhóm www-data bằng lệnh sau:
Mã:
make install-groups-users
usermod -a -G nagios www-data
Tiếp theo, cài đặt Nagios Core bằng lệnh sau lệnh:
Mã:
make install
Bạn sẽ thấy đầu ra sau:
Mã:
*** Main program, CGIs and HTML files installed ***
You can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options): make install-init - This installs the init script in /lib/systemd/system make install-commandmode - This installs and configures permissions on the directory for holding the external command file make install-config - This installs sample config files in /usr/local/nagios/etc
make[1]: Leaving directory '/root/nagios-4.4.7'
Sau khi Nagios Core được cài đặt, bạn sẽ cần tạo tệp dịch vụ systemd để quản lý dịch vụ Nagios. Bạn có thể tạo tập lệnh khởi tạo systemd bằng lệnh sau:
Mã:
make install-daemoninit
Tiếp theo, thiết lập quyền thích hợp cho thư mục cài đặt Nagios bằng lệnh sau:
Mã:
make install-commandmode
Tiếp theo, tạo tệp cấu hình Nagios mẫu bằng lệnh sau lệnh:
Mã:
make install-config
Tiếp theo, cài đặt giao diện web Nagios bằng lệnh sau:
Mã:
make install-webconf
a2enmod rewrite cgi
Tiếp theo, khởi động lại dịch vụ Apache để áp dụng thay đổi:
Mã:
systemctl restart apache2
Tiếp theo, cài đặt chủ đề cho giao diện web Nagios.
Mã:
make install-exfoliation

Tạo Người dùng quản trị Nagios​

Tiếp theo, bạn sẽ cần tạo một người dùng quản trị để truy cập vào giao diện web Nagios. Bạn có thể tạo mật khẩu này bằng lệnh sau:
Mã:
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
Đặt mật khẩu mong muốn như hiển thị bên dưới:
Mã:
New password:
Re-type new password:
Adding password for user nagiosadmin

Cài đặt Nagios Plugin​

Tiếp theo, bạn sẽ cần tải xuống Nagios plugin để giám sát các dịch vụ hệ thống. Bạn có thể tải xuống từ kho lưu trữ Git bằng lệnh sau:
Mã:
wget https://github.com/nagios-plugins/nagios-plugins/releases/download/release-2.4.0/nagios-plugins-2.4.0.tar.gz
Sau khi tải xuống, hãy giải nén tệp đã tải xuống bằng lệnh sau:
Mã:
tar -xvzf nagios-plugins-2.4.0.tar.gz
Tiếp theo, hãy thay đổi thư mục thành thư mục đã giải nén và định cấu hình thư mục đó bằng lệnh sau lệnh:
Mã:
cd nagios-plugins-2.4.0
./configure --with-nagios-user=nagios --with-nagios-group=nagios
Tiếp theo, cài đặt bằng lệnh sau:
Mã:
make
make install
Tiếp theo, xác minh các tệp cấu hình Nagios mẫu trước khi khởi động bằng lệnh sau lệnh:
Mã:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nếu mọi thứ ổn, bạn sẽ nhận được kết quả sau:
Mã:
Running pre-flight check on configuration data...
Checking objects...	Checked 8 services.	Checked 1 hosts.	Checked 1 host groups.	Checked 0 service groups.	Checked 1 contacts.	Checked 1 contact groups.	Checked 24 commands.	Checked 5 time periods.	Checked 0 host escalations.	Checked 0 service escalations.
Checking for circular paths...	Checked 1 hosts	Checked 0 service dependencies	Checked 0 host dependencies	Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check

Khởi động Dịch vụ Nagios​

Bây giờ, hãy khởi động dịch vụ Nagios và cho phép dịch vụ này khởi động khi khởi động lại hệ thống bằng lệnh sau lệnh:
Mã:
systemctl start nagios
systemctl enable nagios
Bạn cũng có thể kiểm tra trạng thái của dịch vụ Nagios bằng lệnh sau:
Mã:
systemctl status nagios
Bạn sẽ nhận được thông báo sau đầu ra:
Mã:
? nagios.service - Nagios Core 4.4.7 Loaded: loaded (/lib/systemd/system/nagios.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2022-05-04 17:18:52 UTC; 1s ago Docs: https://www.nagios.org/documentation Process: 15854 ExecStartPre=/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg (code=exited, status=0/SUCCESS) Process: 15855 ExecStart=/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg (code=exited, status=0/SUCCESS) Main PID: 15856 (nagios) Tasks: 6 (limit: 2241) Memory: 5.2M CPU: 355ms CGroup: /system.slice/nagios.service ??15856 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg ??15857 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh ??15858 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh ??15859 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh ??15860 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh ??15861 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
June 12 06:10:52 jellyfish nagios[15856]: qh: core query handler registered
June 12 06:10:52 jellyfish nagios[15856]: qh: echo service query handler registered
June 12 06:10:52 jellyfish nagios[15856]: qh: help for the query handler registered
June 12 06:10:52 jellyfish nagios[15856]: wproc: Successfully registered manager as @wproc with query handler
June 12 06:10:52 jellyfish nagios[15856]: wproc: Registry request: name=Core Worker 15858;pid=15858
June 12 06:10:52 jellyfish nagios[15856]: wproc: Registry request: name=Core Worker 15857;pid=15857
June 12 06:10:52 jellyfish nagios[15856]: wproc: Registry request: name=Core Worker 15860;pid=15860
June 12 06:10:52 jellyfish nagios[15856]: wproc: Registry request: name=Core Worker 15859;pid=15859
June 12 06:10:53 jellyfish nagios[15856]: Successfully launched command file worker with pid 15861
June 12 06:10:53 jellyfish nagios[15856]: HOST ALERT: localhost;DOWN;SOFT;1;(No output on stdout)

Truy cập Giao diện Web Nagios​

Bây giờ, hãy mở trình duyệt web của bạn và nhập URL . Bạn sẽ được chuyển hướng đến màn hình đăng nhập Nagios:


data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20width=%22750%22%20height=%22298%22%3E%3C/svg%3E


Cung cấp tên người dùng, mật khẩu quản trị Nagios của bạn và nhấp vào nút OK. Bạn sẽ thấy bảng điều khiển mặc định của Nagios trên màn hình sau:


data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20width=%22750%22%20height=%22372%22%3E%3C/svg%3E

Kết luận​

Xin chúc mừng! Bạn đã cài đặt thành công phiên bản Nagios Core mới nhất trên máy chủ Ubuntu 22.04. Bây giờ bạn có thể triển khai Nagios trong cơ sở hạ tầng của mình, thêm máy chủ giám sát và bắt đầu giám sát chúng thông qua trình duyệt web. Hãy thoải mái hỏi tôi nếu bạn có bất kỳ câu hỏi nào.
 
Back
Bên trên