Skip to the content.

Tìm hiểu quản lý hệ thống

I.Đặt lịch.

1. Đặt lịch một lần với at

2.Cron

II. Loging

1.Login Logging

2. syslogd

Nguồn sinh ra log

Facility Number Nguồn tạo log Ý nghĩa
0 kernel Những log mà do kernel sinh ra
1 user Log ghi lại cấp độ người dùng
2 mail Log của hệ thống mail
3 daemon Log của các tiến trình trên hệ thống
4 auth Log từ quá trình đăng nhập hệ hoặc xác thực hệ thống
5 syslog Log từ chương trình syslogd
6 lpr Log từ quá trình in ấn
7 news Thông tin từ hệ thống
8 uucp Log UUCP subsystem
9   Clock deamon
10 authpriv Quá trình đăng nhập hoặc xác thực hệ thống
11 ftp Log của FTP deamon
12   Log từ dịch vụ NTP của các subserver
13   Kiểm tra đăng nhập
14   Log cảnh báo hệ thống
15 cron Log từ clock daemon
16 - 23 local 0 -local 7 Log dự trữ cho sử dụng nội bộ

Mức độ cảnh bảo

Code Mức cảnh báo Ý nghĩa
0 emerg Thông báo tình trạng khẩn cấp
1 alert Hệ thống cần can thiệp ngay
2 crit Tình trạng nguy kịch
3 error Thông báo lỗi đối với hệ thống
4 warn Mức cảnh báo đối với hệ thống
5 notice Chú ý đối với hệ thống
6 info Thông tin của hệ thống
7 debug Quá trình kiểm tra hệ thống

3. Xem log với tail -f

4.rotating log

III. Memory managerment.

1./proc/meminfo

Hiển thị file /proc/meminfo sẽ hiển thị thông tin về memory trên Linux.

[root@cen3 ~]# cat /proc/meminfo
MemTotal:         997956 kB
MemFree:          768100 kB
MemAvailable:     746548 kB
Buffers:            2092 kB
Cached:            84868 kB
SwapCached:            0 kB
...

2.free

free là lệnh để hiển thị một số thông tin trong /proc/meminfo ở dạng có dễ đọc hơn. Dùng lần lượt các tùy chọn -b, -k, -m, -g để đọc các thông số ở dạng byte, kilobyte, megabyte, gigabyte. Ví dụ:

root@m4700:~# free -m
              total        used        free      shared  buff/cache   available
Mem:          15943        3442        8631        1516        3869       10836
Swap:          9764           0        9764

Trong đó, dòng Mem là thông tin về RAM vật lý và dòng Swap chứa các thông tin về bộ nhớ Swap bao gồm:

3. Quản lý swap.

3.1.Swap

3.2.Tạo phân vùn swap.

IV.Resource mornitoring.

Các tài nguyên cơ bản cần theo dõi là cpu,network,bộ nhớ ram, storage.

1.top

Công cụ top giúp monitor ram, cpu, swap. top sẽ tự động làm mới. Trong top có thể sử dụng nhiều câu lệnh như k để kill tiến trình, tm để chỉnh định dạng thông số task,cpu và memory,…

top - 09:52:19 up 58 min,  1 user,  load average: 0,57, 0,72, 0,84
Tasks: 371 total,   2 running, 301 sleeping,   0 stopped,   0 zombie
%Cpu(s):  7,9 us,  1,3 sy,  0,0 ni, 90,8 id,  0,0 wa,  0,0 hi,  0,0 si,  0,0 st
KiB Mem : 16326272 total,  5512916 free,  3282204 used,  7531152 buff/cache
KiB Swap:  9999356 total,  9999356 free,        0 used. 12207280 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                              
  351 lamth     20   0 1091536 284576  94740 S  32,5  1,7   8:17.47 chrome                               
 2179 lamth     20   0 3191304 166280  82052 S  14,6  1,0   1:30.69 Telegram                             
 3370 lamth     20   0  606140 125848  90984 S   7,0  0,8   2:32.31 Xorg                                 
 5023 lamth     20   0 1121096  89512  51464 S   4,0  0,5   0:35.15 python                               
 3586 lamth      9 -11 2542932  13060   9812 S   3,3  0,1   1:28.97 pulseaudio                           
 3576 lamth     20   0 4189732 427584  78020 S   3,0  2,6   3:32.87 gnome-shell  

2.free

Lệnh free là lệnh phổ biến để monitor bộ nhớ trên Linux. Bạn có thể hiển thị thông tin lệnh free mỗi x giây với -s option.

$ free -om -s 10
total used free shared buffers cached
Mem: 249 222 27 0 50 109
Swap: 511 0 511
total used free shared buffers cached
Mem: 249 222 27 0 50 109
Swap: 511 0 511

3.watch

Lệnh watch là lệnh dùng để theo dõi output của một lệnh bằng cách thức hiện câu lệnh đó lặp lai say một khoảng thời gian. Ví dụ một trường hợp thường dùng là dùng vơi lệnh free để theo dõi bộ nhớ liên tục.

[paul@RHELv4u3 ~]$ watch -d -n 3 free -om
...
Every 3.0s: free -om Sat Jan 27 12:13:03 2007
total used free shared buffers cached
Mem: 249 230 19 0 56 109
Swap: 511 0 511

4.vmstat

Để theo dõi số liệu thống kê CPU, disk và bộ nhớ trong một dòng dùng vmstat. Ví dụ bên dưới hiển thị vmstat chạy cứ sau hai giây 100 lần (hoặc cho đến khi Ctrl-C). Bên dưới r, bạn thấy số lượng tiến trình đang chờ CPU, sleep process ở dưới b. Swap được sử dụng (swpd) không đổi ở mức 144 kilobyte, bộ nhớ trống giảm từ 16,7 MB đến 12,9 MB,…

$ vmstat 2 100
procs ----------memory--------- --swap-- ---io--- --system-- ---cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 144 16708 58212 111612 0 0 3 4 75 62 0 1 99 0
0 0 144 16708 58212 111612 0 0 0 0 976 22 0 0 100 0
0 0 144 16708 58212 111612 0 0 0 0 958 14 0 1 99 0
1 0 144 16528 58212 111612 0 0 0 18 1432 7417 1 32 66 0
1 0 144 16468 58212 111612 0 0 0 0 2910 20048 4 95 1 0
1 0 144 16408 58212 111612 0 0 0 0 3210 19509 4 97 0 0
1 0 144 15568 58816 111612 0 0 300 1632 2423 10189 2 62 0 36
0 1 144 13648 60324 111612 0 0 754 0 1910 2843 1 27 0 72
0 0 144 12928 60948 111612 0 0 312 418 1346 1258 0 14 57 29
0 0 144 12928 60948 111612 0 0 0 0 977 19 0 0 100 0
0 0 144 12988 60948 111612 0 0 0 0 977 15 0 0 100 0
0 0 144 12988 60948 111612 0 0 0 0 978 18 0 0 100 0

5.iostat

Công cụ iostat hiển thị trạng thái disk và cpu. Option -d để chỉ hiển thị thông tin của disk.

lamth@m4700:~$ iostat -d
Linux 4.15.0-51-generic (m4700) 	19/06/2019 	_x86_64_	(8 CPU)

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
loop0             0,05         0,25         0,00       1268          0
loop1             0,03         0,24         0,00       1176          0
loop2             4,93         5,13         0,00      25664          0
loop3             0,01         0,07         0,00        328          0
loop4             0,01         0,02         0,00        112          0
loop5             0,01         0,03         0,00        132          0
loop6             0,00         0,01         0,00         54          0
loop7             0,01         0,02         0,00        121          0
sda               0,04         1,67         0,00       8372          0
sdb              25,89       446,51      1100,21    2234203    5505152

6.mpstat

mpstat là lệnh dùng để hiển thị trạng thái về cpu, hiển thị trạng thái của tất cả các cpu bằng cách thêm option -P ALL, hoặc có thể chỉ hiển thị một cpu nào đó.

lamth@m4700:~$ mpstat -P ALL
Linux 4.15.0-51-generic (m4700) 	19/06/2019 	_x86_64_	(8 CPU)

10:22:02     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
10:22:02     all    6,44    0,02    1,66    0,16    0,00    0,01    0,00    0,00    0,00   91,70
10:22:02       0    6,79    0,04    1,54    0,07    0,00    0,03    0,00    0,00    0,00   91,53
10:22:02       1    6,66    0,03    1,48    0,05    0,00    0,02    0,00    0,00    0,00   91,75
10:22:02       2    6,46    0,00    1,51    0,10    0,00    0,04    0,00    0,00    0,00   91,88
10:22:02       3    6,96    0,01    1,61    0,62    0,00    0,02    0,00    0,00    0,00   90,79
10:22:02       4    6,21    0,00    1,46    0,14    0,00    0,00    0,00    0,00    0,00   92,19
10:22:02       5    5,74    0,00    2,32    0,10    0,00    0,00    0,00    0,00    0,00   91,84
10:22:02       6    6,36    0,08    1,61    0,11    0,00    0,00    0,00    0,00    0,00   91,84
10:22:02       7    6,33    0,00    1,78    0,07    0,00    0,00    0,00    0,00    0,00   91,82
lamth@m4700:~$ 

7.nmon và htop

nmonhtop cũng là hai công cụ phổ biến dùng để giám sát, quản lý các thông số tài nguyên của hệ thống.

V. Cấu hình rsyslog.

File cấu hình rsyslog.

File cấu hình và khai báo rule trong centos.

# rsyslog configuration file

# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html

#### MODULES ####

# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark  # provides --MARK-- message capability

# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514


#### GLOBAL DIRECTIVES ####

# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog

# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on

# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf

# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on

# File to store the position in the journal
$IMJournalStateFile imjournal.state


#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog


# Log cron stuff
cron.*                                                  /var/log/cron

# Everybody gets emergency messages
*.emerg                                                 :omusrmsg:*

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log


# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g   # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList   # run asynchronously
#$ActionResumeRetryCount -1    # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514
# ### end of the forwarding rule ###

Một rule có hai trường là Selector và Action. Trong đó:

Ví dụ

cron.info       /var/log/cron

Rule trên sẽ chọn log sinh ra bởi tiến trình cron trong hệ thống ở tất cả mức độ cảnh báo từ mức info đổ lên và lưu vào file /var/log/cron. Một số ký tự đặc biệt như * là chọn tất cả, ! chọn tất cả trừ mục sau dấu chấm than, = chỉ chọn mục sau dấu bằng.

Ví dụ:

*.info;mail.none;user.!notice      /var/log/message

Rule trên sẽ chọn log sinh ra từ tất cả các nguồn và từ mức độ info trở lên nhưng không lưu log của mail và không lưu log mức độ notice của user. Sau đó lưu vào file /var/log/message.

Rotating log

Roting log là quá trình giúp quản lý log bằng cách đổi tên các file log cũ để lưu log mới hoặc có thể lên lịch để xóa các log cũ sau một khoảng thời gian nhất định. Tiện ích dùng để rotating log là logrotate. File cấu hình /etc/logrotate.conf, ngoài ra còn có thư mục /etc/logrotate.d chứa các cấu hình rotate log cho những phần mềm được chay cùng file logrotate.conf. Ví dụ về file `logrotate.conf.

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
	minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

Trong ví dụ trên:

Mặc định logrotate sẽ được chạy định kỳ mỗi ngày một lần với file thực thi logrotate trong /etc/cron.dayly. Nhưng trong file cấu hình logrotate.conf cấu hình là hàng tuần thì hàng tuần logrotate mới rotage file chứ không phải là hàng ngày.