【0611】監控系統狀態、zabbix

5.1~5.9 監控 Linux 系統狀態

  • 監控 CPU 的命令

  • w 查看系統負載

[root@arslinux-01 ~]# w
20:13:49 up 1 min,  1 user,  load average: 1.26, 0.80, 0.31
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.194.1    20:13    5.00s  0.09s  0.01s w

1分鐘內,5分鐘內,15分鐘內

負載值:平均時間內,有多少個活動的進程。數字越大越忙。

正在使用 CPU 或等待 CPU 都屬於活動的進程,無論在排隊還是正在使用都屬於活動進程

  • uptime 查看系統負載

[root@arslinux-01 ~]# uptime
20:25:29 up 2 min,  1 user,  load average: 0.82, 0.67, 0.27
  • top 動態查看系統狀態

可以讓進程按從高到低順序自動排列,默認按 CPU 使用率排列,同樣可以顯示系統負載

[root@arslinux-01 ~]# top
top - 20:28:40 up 5 min,  1 user,  load average: 0.05, 0.37, 0.23
Tasks: 142 total,   2 running, 140 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   995896 total,   257656 free,   569336 used,   168904 buff/cache
KiB Swap:  1999868 total,  1999868 free,        0 used.   254360 avail Mem

PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
25 root      20   0       0      0      0 S  0.3  0.0   0:00.76 kworker/0:1
2947 root      20   0       0      0      0 S  0.3  0.0   0:00.05 xfsaild/sda3
1 root      20   0  125448   3896   2568 S  0.0  0.4   0:02.93 systemd
2 root      20   0       0      0      0 S  0.0  0.0   0:00.01 kthreadd
3 root      20   0       0      0      0 S  0.0  0.0   0:00.29 ksoftirqd/0
4 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0
5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
9 root      20   0       0      0      0 R  0.0  0.0   0:01.32 rcu_sched
10 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 lru-add-drain
11 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 watchdog/0
13 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
14 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns
15 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd

CPU 使用率 = 100% - id值

RES 進程佔用物理內存的大小

top 命令不僅要關注 Tasks 、%CPU 列,同樣要關注下方 %CPU、%MEM、RES 列

-- 每 3 秒刷新一次

-- 按 M 鍵更改未按內存使用率大小排列

-- 按數字 1 可以查看所有 CPU 的使用率詳情(多核)

[root@arslinux-01 ~]# top
top - 20:33:42 up 10 min,  1 user,  load average: 0.00, 0.14, 0.17
Tasks: 142 total,   1 running, 141 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.7 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   995896 total,   255580 free,   571348 used,   168968 buff/cache
KiB Swap:  1999868 total,  1999868 free,        0 used.   252320 avail Mem

PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
7479 mysql     20   0 1302780 454812   6204 S  0.3 45.7   0:05.31 mysqld
7148 root      20   0  573920  17084   6008 S  0.0  1.7   0:00.57 tuned
5983 polkitd   20   0  612228  14172   4680 S  0.0  1.4   0:00.10 polkitd
5974 root      20   0  474244   8900   6660 S  0.0  0.9   0:00.40 NetworkManager
6000 root      20   0  301040   6480   5108 S  0.0  0.7   0:01.36 vmtoolsd
5995 root      20   0   99668   6104   4496 S  0.0  0.6   0:00.05 VGAuthService
7687 root      20   0  158864   5548   4212 S  0.0  0.6   0:00.19 sshd
7273 root      20   0  229608   4976   1232 S  0.0  0.5   0:00.25 php-fpm
7319 php-fpm   20   0  229548   4752   1008 S  0.0  0.5   0:00.00 php-fpm
7320 php-fpm   20   0  229548   4752   1008 S  0.0  0.5   0:00.00 php-fpm
7282 php-fpm   20   0  229548   4748   1008 S  0.0  0.5   0:00.00 php-fpm
7283 php-fpm   20   0  229548   4748   1008 S  0.0  0.5   0:00.00 php-fpm
7284 php-fpm   20   0  229548   4748   1008 S  0.0  0.5   0:00.00 php-fpm
7286 php-fpm   20   0  229548   4748   1008 S  0.0  0.5   0:00.00 php-fpm
7289 php-fpm   20   0  229548   4748   1008 S  0.0  0.5   0:00.00 php-fpm
7291 php-fpm   20   0  229548   4748   1008 S  0.0  0.5   0:00.00 php-fpm

按 q 退出

  • top -bn1靜態顯示所有進程情況,也是按 CPU 百分比排序

[root@arslinux-01 ~]# top -bn1
top - 20:36:02 up 12 min,  1 user,  load average: 0.00, 0.09, 0.15
Tasks: 142 total,   1 running, 141 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.9 us,  5.9 sy,  0.0 ni, 88.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   995896 total,   256044 free,   570848 used,   169004 buff/cache
KiB Swap:  1999868 total,  1999868 free,        0 used.   252804 avail Mem

PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
7745 root      20   0  161880   2164   1552 R  6.2  0.2   0:00.02 top
1 root      20   0  125448   3896   2568 S  0.0  0.4   0:02.94 systemd
2 root      20   0       0      0      0 S  0.0  0.0   0:00.01 kthreadd
3 root      20   0       0      0      0 S  0.0  0.0   0:00.36 ksoftirqd/0
5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
9 root      20   0       0      0      0 S  0.0  0.0   0:01.45 rcu_sched
10 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 lru-add-drain
11 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 watchdog/0
13 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
14 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns
15 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd

(內容過多不全顯示)

-- 靜態顯示進程信息方便與在腳本里使用


  • cat /proc/cpuinfo 查看機器有幾個CPU

[root@arslinux-01 ~]# cat /proc/cpuinfo
processor: 0
vendor_id: GenuineIntel
cpu family: 6
model: 78
model name: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
stepping: 3
microcode: 0xc6
cpu MHz: 2399.999
cache size: 3072 KB
physical id: 0
siblings: 1
core id: 0
cpu cores: 1
apicid: 0
initial apicid: 0
fpu: yes
fpu_exception: yes
cpuid level: 22
wp: yes
flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch epb ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt dtherm arat pln pts hwp hwp_notify hwp_act_window hwp_epp spec_ctrl intel_stibp
bogomips: 4799.99
clflush size: 64
cache_alignment: 64
address sizes: 42 bits physical, 48 bits virtual
power management:
  • lscpu查看CPU的信息

[root@arslinux-01 ~]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
座:                 1
NUMA 節點:         1
廠商 ID:           GenuineIntel
CPU 系列:          6
型號:              78
型號名稱:        Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
步進:              3
CPU MHz:             2399.999
CPU max MHz:           0.0000
CPU min MHz:           0.0000
BogoMIPS:            4799.99
超管理器廠商:  VMware
虛擬化類型:     完全
L1d 緩存:          32K
L1i 緩存:          32K
L2 緩存:           256K
L3 緩存:           3072K
NUMA 節點0 CPU:    0
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts nopl xtopology tsc_reliable nonstop_tsc aperfmperf eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch epb ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap xsaveopt dtherm arat pln pts hwp hwp_notify hwp_act_window hwp_epp spec_ctrl intel_stibp


  • 監控內存的命令

  • free 查看內存和 swap 使用情況(默認以 kb 爲單位)

[root@arslinux-01 ~]# free
total        used        free      shared  buff/cache   available
Mem:         995896      570492      256152        7864      169252      253164
Swap:       1999868           0     1999868
  • free -m 按 M 爲單位查看系統內存相關信息

[root@arslinux-01 ~]# free -m
total        used        free      shared  buff/cache   available
Mem:            972         557         250           7         165         247
Swap:          1952           0        1952
  • free -h 自動以單位顯示內存信息

[root@arslinux-01 ~]# free -h
total        used        free      shared  buff/cache   available
Mem:           972M        557M        250M        7.7M        165M        247M
Swap:          1.9G          0B        1.9G
  • free -b 以 B 爲單位顯示內存信息

[root@arslinux-01 ~]# free -b
total        used        free      shared  buff/cache   available
Mem:     1019797504   584146944   262340608     8052736   173309952   259309568
Swap:    2047864832           0  2047864832

只需關注 available,available = free + buff/cache未使用的部分

內存不夠用時,swap 會將內存裏的內容交換到磁盤分區裏

buffer:從 CPU 產生,即將寫入到磁盤裏去的那部分數據

cached:先從磁盤裏讀出來,然後臨時存到內存裏的那部分數據

  • 手動增加 swap

磁盤上模擬出 1G 空間

dd if=/dev/zero of=/bigfile bs=100M count=10

of 目標文件,bs 單個塊大小,count 塊數量

[root@arslinux-01 ~]# dd if=/dev/zero of=/bigfile bs=100M count=10
記錄了10+0 的讀入
記錄了10+0 的寫出
1048576000字節(1.0 GB)已複製,7.92899 秒,132 MB/秒
[root@arslinux-01 ~]# du -sh /bigfile
1000M/bigfile
[root@arslinux-01 ~]# mkswap /bigfile
正在設置交換空間版本 1,大小 = 1023996 KiB
無標籤,UUID=0f334ed6-5c2b-49e6-a226-8afd4989123b
[root@arslinux-01 ~]# chmod 600 /bigfile
[root@arslinux-01 ~]# swapon /bigfile
[root@arslinux-01 ~]# free
total        used        free      shared  buff/cache   available
Mem:         995896      571836      167716        7896      256344      249096
Swap:       3023864           0     3023864


  • 監控磁盤的命令

  • iostat 監控磁盤(需要安裝 sysstat )

[root@arslinux-01 ~]# iostat
Linux 3.10.0-957.el7.x86_64 (arslinux-01) 2019年06月10日 _x86_64_(1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
0.39    0.00    1.23    0.02    0.00   98.36

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               1.79        43.21       294.17     151445    1031077
sdb               0.07         2.63         0.00       9212          0
scd0              0.01         0.29         0.00       1028          0
dm-0              0.02         0.59         0.00       2072          0
  • iostat 1 每隔1秒查詢一次

[root@arslinux-01 ~]# iostat 1
Linux 3.10.0-957.el7.x86_64 (arslinux-01) 2019年06月10日 _x86_64_(1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
0.38    0.00    1.21    0.02    0.00   98.39

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               1.75        42.07       286.38     151481    1031133
sdb               0.07         2.56         0.00       9212          0
scd0              0.00         0.29         0.00       1028          0
dm-0              0.02         0.58         0.00       2072          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
0.00    0.00    0.00    0.00    0.00  100.00

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.00         0.00         0.00          0          0
sdb               0.00         0.00         0.00          0          0
scd0              0.00         0.00         0.00          0          0
dm-0              0.00         0.00         0.00          0          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
1.00    0.00    1.00    0.00    0.00   98.00

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.00         0.00         0.00          0          0
sdb               0.00         0.00         0.00          0          0
scd0              0.00         0.00         0.00          0          0
dm-0              0.00         0.00         0.00          0
  • iostat -d 只查看 device 相關信息

[root@arslinux-01 ~]# iostat -d 1
Linux 3.10.0-957.el7.x86_64 (arslinux-01) 2019年06月10日 _x86_64_(1 CPU)

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               1.69        40.65       276.72     151481    1031220
sdb               0.07         2.47         0.00       9212          0
scd0              0.00         0.28         0.00       1028          0
dm-0              0.02         0.56         0.00       2072          0

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.00         0.00         0.00          0          0
sdb               0.00         0.00         0.00          0          0
scd0              0.00         0.00         0.00          0          0
dm-0              0.00         0.00         0.00          0          0

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.00         0.00         0.00          0          0
sdb               0.00         0.00         0.00          0          0
scd0              0.00         0.00         0.00          0          0
dm-0              0.00         0.00         0.00          0          0
  • iostat -dx 1 加上 x 查看到的信息更全

[root@arslinux-01 ~]# iostat -dx 1
Linux 3.10.0-957.el7.x86_64 (arslinux-01) 2019年06月10日 _x86_64_(1 CPU)

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sda               0.00     0.01    0.96    0.70    39.75   270.58   375.46     0.02   11.54    0.82   26.21   1.13   0.19
sdb               0.00     0.00    0.07    0.00     2.42     0.00    70.59     0.00    0.51    0.51    0.00   0.37   0.00
scd0              0.00     0.00    0.00    0.00     0.27     0.00   114.22     0.00    1.56    1.56    0.00   1.22   0.00
dm-0              0.00     0.00    0.02    0.00     0.54     0.00    48.19     0.00    0.50    0.50    0.00   0.35
   0.00
Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sda               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
sdb               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
scd0              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
dm-0              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00
   0.00
Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sda               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
sdb               0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
scd0              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00
dm-0              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00

關注 %util,磁盤的使用率

  • iostat -d 1 2 顯示 2 次

[root@arslinux-01 ~]# iostat -d  1 2
Linux 3.10.0-957.el7.x86_64 (arslinux-01) 2019年06月10日 _x86_64_(1 CPU)

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               1.58        38.07       259.16     151481    1031222
sdb               0.07         2.32         0.00       9212          0
scd0              0.00         0.26         0.00       1028          0
dm-0              0.02         0.52         0.00       2072          0

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.00         0.00         0.00          0          0
sdb               0.00         0.00         0.00          0          0
scd0              0.00         0.00         0.00          0          0
dm-0              0.00         0.00         0.00          0          0
  • iotop 查看進程使用磁盤的信息,動態顯示,按IO使用率大小排序(安裝 iotop)

1.png

  • vmstat 綜合顯示 CPU、內存、磁盤的信息(用法和 iostat 類似)

[root@arslinux-01 ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
2  0      0 146048    116 273320    0    0    40   237   63  148  1  1 98  0  0
0  0      0 146048    116 273320    0    0     0     0   35   79  0  1 99  0  0
0  0      0 146048    116 273320    0    0     0     0   50   90  0  0 100  0  0
1  0      0 146048    116 273320    0    0     0     0   33   73  0  0 100  0  0
0  0      0 146048    116 273320    0    0     0     0   43   78  0  1 98  1  0
^C
[root@arslinux-01 ~]# vmstat 1 3
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
2  0      0 146196    116 273320    0    0    40   236   63  148  1  1 98  0  0
0  0      0 146164    116 273320    0    0     0     0   38   84  0  0 100  0  0
0  0      0 146164    116 273320    0    0     0     0   50   91  0  1 99  0  0

CPU ——> id

memory——>swpd 非零 或 si so 非零(si = swap in從swap進到內存,so = swap out)

bi 磁盤讀,bo 磁盤寫

r = run,b =block

wa =wait 等待 CPU 的百分比

關注:r、b、si、so、bi、bo、id、wa


  • 監控網卡的命令

  • sar 監控網卡流量(同樣由 sysstat 包安裝)

[root@arslinux-01 ~]# sar -n DEV 1 3
Linux 3.10.0-957.el7.x86_64 (arslinux-01) 2019年06月10日 _x86_64_(1 CPU)

22時31分30秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
22時31分31秒     ens37      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22時31分31秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22時31分31秒     ens33      1.01      0.00      0.06      0.00      0.00      0.00      0.00
22時31分31秒     ens38      0.00      0.00      0.00      0.00      0.00      0.00      0.00

22時31分31秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
22時31分32秒     ens37      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22時31分32秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22時31分32秒     ens33      1.01      1.01      0.06      0.57      0.00      0.00      0.00
22時31分32秒     ens38      0.00      0.00      0.00      0.00      0.00      0.00      0.00

22時31分32秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
22時31分33秒     ens37      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22時31分33秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22時31分33秒     ens33      1.00      1.00      0.06      0.56      0.00      0.00      0.00
22時31分33秒     ens38      0.00      0.00      0.00      0.00      0.00      0.00      0.00

平均時間:     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
平均時間:     ens37      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均時間:        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均時間:     ens33      1.01      0.67      0.06      0.38      0.00      0.00      0.00
平均時間:     ens38      0.00      0.00      0.00      0.00      0.00      0.00      0.00

小常識:

bit 比特(帶寬單位)

Byte 字節(速度傳輸單位)

8bit = 1Byte

/var/log/sa/saxx xx爲日期

  • sar -f /var/log/sa/saxx 查看歷史 sar 記錄

[root@arslinux-01 ~]# sar -f /var/log/sa/sa14
Linux 3.10.0-957.el7.x86_64 (arslinux-01) 2019年05月14日 _x86_64_(1 CPU)

20時44分23秒       LINUX RESTART

20時50分01秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
21時00分01秒     all      5.29      0.00      2.01      0.02      0.00     92.68
21時10分01秒     all      0.08      0.00      0.23      0.01      0.00     99.68
21時20分01秒     all      0.11      0.00      0.28      0.00      0.00     99.60
21時30分01秒     all      0.08      0.00      0.22      0.00      0.00     99.70
21時40分01秒     all      0.04      0.00      0.15      0.00      0.00     99.80
21時50分01秒     all      0.05      0.11      0.56      0.02      0.00     99.26
22時00分01秒     all      0.06      0.00      0.19      0.00      0.00     99.74
22時10分01秒     all      0.09      0.00      0.23      0.01      0.00     99.68
平均時間:     all      0.72      0.01      0.48      0.01      0.00     98.77

-- sar -n DEV -f /var/log/sa/sa14 只看具體日期的網卡流量

  • sar 查看 CPU

[root@arslinux-01 ~]# sar
Linux 3.10.0-957.el7.x86_64 (arslinux-01) 2019年06月10日 _x86_64_(1 CPU)

20時12分31秒       LINUX RESTART

20時23分48秒       LINUX RESTART

20時30分01秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
20時40分01秒     all      0.15      0.00      0.45      0.00      0.00     99.40
20時50分01秒     all      0.08      0.00      0.28      0.00      0.00     99.63
21時00分01秒     all      0.07      0.00      0.26      0.00      0.00     99.67
21時10分01秒     all      0.07      0.00      1.13      0.05      0.00     98.75
21時20分01秒     all      0.02      0.00      0.17      0.00      0.00     99.81
21時30分01秒     all      0.08      0.00      0.27      0.01      0.00     99.65
21時40分01秒     all      1.35      0.02      0.76      0.01      0.00     97.85
21時50分01秒     all      0.08      0.00      0.26      0.00      0.00     99.66
22時00分01秒     all      0.02      0.00      0.17      0.00      0.00     99.81
22時10分01秒     all      0.02      0.00      0.17      0.00      0.00     99.81
22時20分01秒     all      0.01      0.00      0.17      0.00      0.00     99.82
22時30分01秒     all      0.04      0.00      0.20      0.00      0.00     99.76
22時40分01秒     all      0.11      0.00      0.35      0.01      0.00     99.53
平均時間:     all      0.16      0.00      0.36      0.01      0.00     99.47
  • sar -b 查看磁盤io

[root@arslinux-01 ~]# sar -b
Linux 3.10.0-957.el7.x86_64 (arslinux-01) 2019年06月10日 _x86_64_(1 CPU)

20時12分31秒       LINUX RESTART

20時23分48秒       LINUX RESTART

20時30分01秒       tps      rtps      wtps   bread/s   bwrtn/s
20時40分01秒      0.05      0.00      0.05      0.07      0.56
20時50分01秒      0.06      0.01      0.05      0.89      0.53
21時00分01秒      0.05      0.00      0.05      0.00      0.49
21時10分01秒      3.81      0.31      3.50     29.38   3451.05
21時20分01秒      0.05      0.00      0.05      0.12      0.46
21時30分01秒      0.07      0.01      0.06      0.40      0.81
21時40分01秒      1.57      1.04      0.54     47.96     16.79
21時50分01秒      0.05      0.00      0.05      0.00      0.51
22時00分01秒      0.06      0.00      0.06      0.01      0.74
22時10分01秒      0.16      0.11      0.06     21.74      0.63
22時20分01秒      0.03      0.00      0.03      0.00      0.33
22時30分01秒      0.09      0.01      0.08      0.55      1.00
22時40分01秒      0.13      0.04      0.09      3.18     24.33
平均時間:      0.47      0.12      0.36      8.01    267.86
  • sar -q 查看系統負載

[root@arslinux-01 ~]# sar -q
Linux 3.10.0-957.el7.x86_64 (arslinux-01) 2019年06月10日 _x86_64_(1 CPU)

20時12分31秒       LINUX RESTART

20時23分48秒       LINUX RESTART

20時30分01秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
20時40分01秒         0       185      0.00      0.04      0.12         0
20時50分01秒         1       184      0.07      0.03      0.07         0
21時00分01秒         1       184      0.00      0.01      0.05         0
21時10分01秒         1       185      0.00      0.02      0.05         0
21時20分01秒         2       185      0.00      0.01      0.05         0
21時30分01秒         1       185      0.00      0.01      0.05         0
21時40分01秒         0       186      0.00      0.02      0.05         0
21時50分01秒         1       185      0.00      0.01      0.05         0
22時00分01秒         1       184      0.00      0.03      0.05         0
22時10分01秒         1       184      0.00      0.01      0.05         0
22時20分01秒         1       184      0.01      0.02      0.05         0
22時30分01秒         1       184      0.00      0.01      0.05         0
22時40分01秒         1       184      0.00      0.01      0.05         0
平均時間:         1       185      0.01      0.02      0.06         0
  • nload 查看網卡流量(安裝 nload 包,先要安裝 epel-release 包)

2.png

按左右方向鍵切換不同的網卡

  • ethtool 網卡名 查看網卡信息

[root@arslinux-01 ~]# ethtool ens33
Settings for ens33:
    Supported ports: [ TP ]
    Supported link modes:   10baseT/Half 10baseT/Full
                    100baseT/Half 100baseT/Full
                    1000baseT/Full
    Supported pause frame use: No
    Supports auto-negotiation: Yes
    Supported FEC modes: Not reported
    Advertised link modes:  10baseT/Half 10baseT/Full
                    100baseT/Half 100baseT/Full
                    1000baseT/Full
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Advertised FEC modes: Not reported
    Speed: 1000Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: on
    MDI-X: off (auto)
    Supports Wake-on: d
    Wake-on: d
    Current message level: 0x00000007 (7)
                    drv probe link
    Link detected: yes
  • mii-tool 網卡名 查看網卡是否連接

[root@arslinux-01 ~]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok


  • 查看進程相關的命令

  • ps aux 查看系統所有用戶前臺後臺用戶級別的所有進程

  • ps elf 同上類似

  • ll /proc/進程id 查看未知進程的情況(是由哪個命令起來的)

  • ps eLf 查看進程的線程

[root@arslinux-01 ~]# ps -eLf | grep mysql
root       7203      1   7203  0    1 20:23 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/datamysql --pid-file=/data/mysql/arslinux-01.pid
mysql      7479   7203   7479  0   21 20:23 ?        00:00:04 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7691  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7692  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7693  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7694  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7695  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7696  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7697  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7698  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7699  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7700  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7702  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7703  0   21 20:24 ?        00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7704  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7705  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7706  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7707  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7708  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7709  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7710  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
mysql      7479   7203   7711  0   21 20:24 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arslinux-01.err --pid-file=/datamysql/arslinux-01.pid --socket=/tmp/mysql.sock
root       7987   7714   7987  0    1 23:03 pts/0    00:00:00 grep --color=auto mysql

狀態:

S 處於休眠狀態

R 正在運行中

s 父進程(有子進程)

l 多線程

< 優先級高

N 優先級低

+ 前臺的進程(不帶+就是後臺進程)

Z 殭屍進程

關注點:

%CPU、%MEM、RSS(進程佔用的固定內存量)、STAT

擴展連接:https://www.cnblogs.com/huangmr0811/p/5570999.html


  • 查看網絡連接狀況的命令

  • netstat 查看網絡連接狀況

[root@arslinux-01 ~]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:38825           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7174/nginx: master
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      7177/rpc.mountd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7150/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7557/master
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      7174/nginx: master
tcp        0      0 0.0.0.0:44352           0.0.0.0:*               LISTEN      7159/rpc.statd
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -
tcp6       0      0 :::3306                 :::*                    LISTEN      7479/mysqld
tcp6       0      0 :::38639                :::*                    LISTEN      -
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::20048                :::*                    LISTEN      7177/rpc.mountd
tcp6       0      0 :::22                   :::*                    LISTEN      7150/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      7557/master
tcp6       0      0 :::2049                 :::*                    LISTEN      -
tcp6       0      0 :::48737                :::*                    LISTEN      7159/rpc.statd
udp        0      0 127.0.0.1:975           0.0.0.0:*                           7159/rpc.statd
udp        0      0 0.0.0.0:2049            0.0.0.0:*                           -
udp        0      0 0.0.0.0:60468           0.0.0.0:*                           7159/rpc.statd
udp        0      0 0.0.0.0:20048           0.0.0.0:*                           7177/rpc.mountd
udp        0      0 0.0.0.0:111             0.0.0.0:*                           1/systemd
udp        0      0 127.0.0.1:323           0.0.0.0:*                           6039/chronyd
udp        0      0 0.0.0.0:58247           0.0.0.0:*                           -
udp        0      0 0.0.0.0:930             0.0.0.0:*                           5894/rpcbind
udp6       0      0 :::2049                 :::*                                -
udp6       0      0 :::20048                :::*                                7177/rpc.mountd
udp6       0      0 :::111                  :::*                                1/systemd
udp6       0      0 :::48248                :::*                                7159/rpc.statd
udp6       0      0 :::36657                :::*                                -
udp6       0      0 ::1:323                 :::*                                6039/chronyd
udp6       0      0 :::930                  :::*                                5894/rpcbind
raw6       0      0 :::58                   :::*                    7           5974/NetworkManager
raw6       0      0 :::58                   :::*                    7           5974/NetworkManager
raw6       0      0 :::58                   :::*                    7           5974/NetworkManager
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     41459    7557/master          public/pickup
unix  2      [ ACC ]     STREAM     LISTENING     40742    7243/gssproxy        /run/gssproxy.sock
unix  2      [ ACC ]     STREAM     LISTENING     41463    7557/master          public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     41466    7557/master          public/qmgr
unix  2      [ ACC ]     STREAM     LISTENING     41488    7557/master          public/flush
unix  2      [ ACC ]     STREAM     LISTENING     41503    7557/master          public/showq
unix  2      [ ACC ]     STREAM     LISTENING     41470    7557/master          private/tlsmgr
unix  2      [ ACC ]     STREAM     LISTENING     21315    1/systemd            /run/lvm/lvmetad.socket
unix  2      [ ACC ]     STREAM     LISTENING     41797    7479/mysqld          /tmp/mysql.sock
unix  2      [ ACC ]     STREAM     LISTENING     41473    7557/master          private/rewrite
unix  2      [ ACC ]     STREAM     LISTENING     41476    7557/master          private/bounce
unix  2      [ ACC ]     STREAM     LISTENING     41479    7557/master          private/defer
unix  2      [ ACC ]     STREAM     LISTENING     41482    7557/master          private/trace
unix  2      [ ACC ]     STREAM     LISTENING     40741    7243/gssproxy        /var/lib/gssproxy/default.sock
unix  2      [ ACC ]     STREAM     LISTENING     32898    1/systemd            /run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     32901    1/systemd            /var/run/rpcbind.sock
unix  2      [ ACC ]     STREAM     LISTENING     21410    1/systemd            /run/lvm/lvmpolld.socket
unix  2      [ ACC ]     STREAM     LISTENING     41485    7557/master          private/verify
unix  2      [ ACC ]     STREAM     LISTENING     41491    7557/master          private/proxymap
unix  2      [ ACC ]     STREAM     LISTENING     41494    7557/master          private/proxywrite
unix  2      [ ACC ]     STREAM     LISTENING     41497    7557/master          private/smtp
unix  2      [ ACC ]     STREAM     LISTENING     41500    7557/master          private/relay
unix  2      [ ACC ]     STREAM     LISTENING     41506    7557/master          private/error
unix  2      [ ACC ]     STREAM     LISTENING     41509    7557/master          private/retry
unix  2      [ ACC ]     STREAM     LISTENING     41512    7557/master          private/discard
unix  2      [ ACC ]     STREAM     LISTENING     41515    7557/master          private/local
unix  2      [ ACC ]     STREAM     LISTENING     40908    7273/php-fpm: maste  /tmp/arslinux.sock
unix  2      [ ACC ]     STREAM     LISTENING     41518    7557/master          private/virtual
unix  2      [ ACC ]     STREAM     LISTENING     41521    7557/master          private/lmtp
unix  2      [ ACC ]     STREAM     LISTENING     41524    7557/master          private/anvil
unix  2      [ ACC ]     STREAM     LISTENING     40911    7273/php-fpm: maste  /tmp/php-fcgi.sock
unix  2      [ ACC ]     STREAM     LISTENING     41527    7557/master          private/scache
unix  2      [ ACC ]     SEQPACKET  LISTENING     21461    1/systemd            /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     35295    5995/VGAuthService   /var/run/vmware/guestServicePipe
unix  2      [ ACC ]     STREAM     LISTENING     8946     1/systemd            /run/systemd/journal/stdout
unix  2      [ ACC ]     STREAM     LISTENING     21239    1/systemd            /run/systemd/private
  • netstat -lntp 只查看 tcp 監聽的連接狀態

[root@arslinux-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:38825           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7174/nginx: master
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      7177/rpc.mountd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7150/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7557/master
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      7174/nginx: master
tcp        0      0 0.0.0.0:44352           0.0.0.0:*               LISTEN      7159/rpc.statd
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -
tcp6       0      0 :::3306                 :::*                    LISTEN      7479/mysqld
tcp6       0      0 :::38639                :::*                    LISTEN      -
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::20048                :::*                    LISTEN      7177/rpc.mountd
tcp6       0      0 :::22                   :::*                    LISTEN      7150/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      7557/master
tcp6       0      0 :::2049                 :::*                    LISTEN      -
tcp6       0      0 :::48737                :::*                    LISTEN      7159/rpc.statd
  • netstat -lnup 只查看 udp 監聽的連接狀態

  • netstat -lntup 查看 tcp 和 udp 監聽的連接狀態

  • netstat -an 查看所有的網絡連接,包括監聽的,包括已連接的

  • ss -an 類似於 netstat -an,顯示更快,如果 netstat -an 比較慢,可以嘗試 ss -an

3.png

ESTABLISHED 建立連接

TIME_WAIT 要斷還沒斷開就是

擴展連接:https://blog.csdn.net/m0_37556444/article/details/83000553


  • 網卡流量異常,通過抓包來分析

  • tcpdump 抓包工具(安裝 tcpdump)

-nn    數字的形式顯示 ip 和 port

-i        指定端口

port    指定端口號

-c        指定抓多少包

-w        指定寫入到哪個文件裏去

not port 不要某個端口,多個端口可以 not port and not port

not host 不要某個 ip 的

[root@arslinux-01 ~]# tcpdump -nn -i ens33 port 80
  • tcpdump -r 讀取 .cap 文件

  • 可以用windows上 wireshark 分析 .cap 文件

  • tshark 抓包工具(需要安裝 wireshark 包)和 tcpdump 用法類似

[root@arslinux-01 ~]# tshark -nn -i ens33
Running as user "root" and group "root". This could be dangerous.
Capturing on 'ens33'
1 0.000000000 192.168.194.1 -> 239.255.255.250 SSDP 216 M-SEARCH * HTTP/1.1
2 0.506531192 192.168.194.130 -> 192.168.194.1 SSH 170 Encrypted response packet len=116
3 0.555192741 192.168.194.1 -> 192.168.194.130 TCP 60 4518 > 22 [ACK] Seq=1 Ack=117 Win=2048 Len=0
4 1.000574486 192.168.194.1 -> 239.255.255.250 SSDP 216 M-SEARCH * HTTP/1.1
5 1.253766432 192.168.194.130 -> 192.168.194.1 SSH 362 Encrypted response packet len=308
6 1.295522376 192.168.194.1 -> 192.168.194.130 TCP 60 4518 > 22 [ACK] Seq=1 Ack=425 Win=2047 Len=0
7 1.797154561 192.168.194.130 -> 192.168.194.1 SSH 282 Encrypted response packet len=228
8 1.838083781 192.168.194.1 -> 192.168.194.130 TCP 60 4518 > 22 [ACK] Seq=1 Ack=653 Win=2053 Len=0
9 2.003689876 192.168.194.1 -> 239.255.255.250 SSDP 216 M-SEARCH * HTTP/1.1
10 2.506430635 192.168.194.130 -> 192.168.194.1 SSH 362 Encrypted response packet len=308
11 2.547345210 192.168.194.1 -> 192.168.194.130 TCP 60 4518 > 22 [ACK] Seq=1 Ack=961 Win=2051 Len=0
12 3.005064119 192.168.194.1 -> 239.255.255.250 SSDP 216 M-SEARCH * HTTP/1.1
1 packet dropped
12 packets captured

tshark -i 端口號 -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"

結果和訪問日誌一樣


5.10 監控介紹

爲什麼要做監控?

可以提前發現故障隱患,第一時間解決掉,把影響降低。

常見的監控軟件:

cacti(專業網絡設備監控軟件)、nagios、zabbix、open-falcon、prometheus、grafana

zabbix 操作方便,在瀏覽器操作;可以自定定義監控腳本;可以方便查看趨勢圖形圖像;可以查看歷史區間內的各項指標


5.11~5.12 安裝zabbix4.0

zabbix 官網:www.zabbix.com

4.png5.png

6.png

  • 前期準備

1、安裝 zabbix 的 yum 源

[root@arslinux-01 ~]# rpm -ivh 
https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm

2、查看可用的 yum 源

[root@arslinux-01 ~]# yum list|grep zabbix
zabbix-release.noarch                   4.0-1.el7                      ×××talled
iksemel.x86_64                          1.4-2.el7.centos               zabbix-non-supported
iksemel-devel.x86_64                    1.4-2.el7.centos               zabbix-non-supported
iksemel-utils.x86_64                    1.4-2.el7.centos               zabbix-non-supported
pcp-export-pcp2zabbix.x86_64            4.1.0-5.el7_6                  updates
pcp-export-zabbix-agent.x86_64          4.1.0-5.el7_6                  updates
python-pyzabbix.noarch                  0.7.3-2.el7                    epel
python2-zabbix-api-erigones.noarch      1.2.4-3.el7                    epel
python36-zabbix-api-erigones.noarch     1.2.4-3.el7                    epel
uwsgi-stats-pusher-zabbix.x86_64        2.0.17.1-2.el7                 epel
zabbix-agent.x86_64                     4.0.9-3.el7                    zabbix
zabbix-get.x86_64                       4.0.9-3.el7                    zabbix
zabbix-java-gateway.x86_64              4.0.9-3.el7                    zabbix
zabbix-proxy-mysql.x86_64               4.0.9-3.el7                    zabbix
zabbix-proxy-pgsql.x86_64               4.0.9-3.el7                    zabbix
zabbix-proxy-sqlite3.x86_64             4.0.9-3.el7                    zabbix
zabbix-sender.x86_64                    4.0.9-3.el7                    zabbix
zabbix-server-mysql.x86_64              4.0.9-3.el7                    zabbix
zabbix-server-pgsql.x86_64              4.0.9-3.el7                    zabbix
zabbix-web.noarch                       4.0.9-3.el7                    zabbix
zabbix-web-japanese.noarch              4.0.9-3.el7                    zabbix
zabbix-web-mysql.noarch                 4.0.9-3.el7                    zabbix
zabbix-web-pgsql.noarch                 4.0.9-3.el7                    zabbix
zabbix22.x86_64                         2.2.23-1.el7                   epel
zabbix22-agent.x86_64                   2.2.23-1.el7                   epel
zabbix22-dbfiles-mysql.noarch           2.2.23-1.el7                   epel
zabbix22-dbfiles-pgsql.noarch           2.2.23-1.el7                   epel
zabbix22-dbfiles-sqlite3.noarch         2.2.23-1.el7                   epel
zabbix22-proxy.noarch                   2.2.23-1.el7                   epel
zabbix22-proxy-mysql.x86_64             2.2.23-1.el7                   epel
zabbix22-proxy-pgsql.x86_64             2.2.23-1.el7                   epel
zabbix22-proxy-sqlite3.x86_64           2.2.23-1.el7                   epel
zabbix22-server.noarch                  2.2.23-1.el7                   epel
zabbix22-server-mysql.x86_64            2.2.23-1.el7                   epel
zabbix22-server-pgsql.x86_64            2.2.23-1.el7                   epel
zabbix22-web.noarch                     2.2.23-1.el7                   epel
zabbix22-web-mysql.noarch               2.2.23-1.el7                   epel
zabbix22-web-pgsql.noarch               2.2.23-1.el7                   epel
zabbix30.x86_64                         3.0.22-2.el7                   epel
zabbix30-agent.x86_64                   3.0.22-2.el7                   epel
zabbix30-dbfiles-mysql.noarch           3.0.22-2.el7                   epel
zabbix30-dbfiles-pgsql.noarch           3.0.22-2.el7                   epel
zabbix30-dbfiles-sqlite3.noarch         3.0.22-2.el7                   epel
zabbix30-proxy.noarch                   3.0.22-2.el7                   epel
zabbix30-proxy-mysql.x86_64             3.0.22-2.el7                   epel
zabbix30-proxy-pgsql.x86_64             3.0.22-2.el7                   epel
zabbix30-proxy-sqlite3.x86_64           3.0.22-2.el7                   epel
zabbix30-server.noarch                  3.0.22-2.el7                   epel
zabbix30-server-mysql.x86_64            3.0.22-2.el7                   epel
zabbix30-server-pgsql.x86_64            3.0.22-2.el7                   epel
zabbix30-web.noarch                     3.0.22-2.el7                   epel
zabbix30-web-mysql.noarch               3.0.22-2.el7                   epel
zabbix30-web-pgsql.noarch               3.0.22-2.el7                   epel
zabbix40.x86_64                         4.0.6-1.el7                    epel
zabbix40-agent.x86_64                   4.0.6-1.el7                    epel
zabbix40-dbfiles-mysql.noarch           4.0.6-1.el7                    epel
zabbix40-dbfiles-pgsql.noarch           4.0.6-1.el7                    epel
zabbix40-dbfiles-sqlite3.noarch         4.0.6-1.el7                    epel
zabbix40-proxy.noarch                   4.0.6-1.el7                    epel
zabbix40-proxy-mysql.x86_64             4.0.6-1.el7                    epel
zabbix40-proxy-pgsql.x86_64             4.0.6-1.el7                    epel
zabbix40-proxy-sqlite3.x86_64           4.0.6-1.el7                    epel
zabbix40-server.noarch                  4.0.6-1.el7                    epel
zabbix40-server-mysql.x86_64            4.0.6-1.el7                    epel
zabbix40-server-pgsql.x86_64            4.0.6-1.el7                    epel
zabbix40-web.noarch                     4.0.6-1.el7                    epel
zabbix40-web-mysql.noarch               4.0.6-1.el7                    epel
zabbix40-web-pgsql.noarch               4.0.6-1.el7                    epel

3、需要安裝的包如下:

zabbix-agent、zabbix-get、zabbix-server-mysql、zabbix-web、zabbix-web-mysql

4、安裝需要裝的所有包

[root@arslinux-01 ~]# yum ×××tall zabbix-agent zabbix-get zabbix-server-mysql zabbix-web zabbix-web-mysql

5、查看端口

[root@arslinux-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:40647           0.0.0.0:*               LISTEN      7181/rpc.statd
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7201/nginx: master
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      7199/rpc.mountd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7173/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7584/master
tcp        0      0 0.0.0.0:45177           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      7201/nginx: master
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -
tcp6       0      0 :::3306                 :::*                    LISTEN      7516/mysqld
tcp6       0      0 :::46092                :::*                    LISTEN      7181/rpc.statd
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::20048                :::*                    LISTEN      7199/rpc.mountd
tcp6       0      0 :::22                   :::*                    LISTEN      7173/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      7584/master
tcp6       0      0 :::2049                 :::*                    LISTEN      -
tcp6       0      0 :::38661                :::*                    LISTEN

6、創建數據庫 zabbix 並配置

[root@arslinux-01 ~]# mysql -uroot -parslinux
Warning: Using a password on the command line interface can be ×××ecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.43-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database zabbix;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on zabbix.* to 'zabbix'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

7、編輯 zabbix server 配置文件

[root@arslinux-01 ~]# vim /etc/zabbix/zabbix_server.conf

DBHost=127.0.0.1

DBName=zabbix

DBPassword=123456

8、啓動 zabbix-server

[root@arslinux-01 ~]# systemctl start zabbix-server
[root@arslinux-01 ~]# ps aux|grep zabbix
root       8078  0.0  0.0 112724   984 pts/0    R+   12:49   0:00 grep --color=auto zabbix

9、程序未啓動,查看日誌文件 /var/log/zabbix/zabbix_server.log

[root@arslinux-01 ~]# cat /var/log/zabbix/zabbix_server.log
8156:20190611:125125.104 [Z3005] query failed: [1146] Table 'zabbix.users' doesn't exist [select userid from users limit 1]

庫裏沒有表,所以無法啓動

10、安裝的 RPM 包中帶有 sql 文件(create.sql),將其導入數據庫

[root@arslinux-01 ~]# ls /usr/share/doc/zabbix-server-mysql-4.0.9/
AUTHORS  ChangeLog  COPYING  create.sql.gz  NEWS  README
[root@arslinux-01 ~]# gzip -d !$
gzip -d /usr/share/doc/zabbix-server-mysql-4.0.9/create.sql.gz
[root@arslinux-01 ~]# ls /usr/share/doc/zabbix-server-mysql-4.0.9/
AUTHORS  ChangeLog  COPYING  create.sql  NEWS  README
[root@arslinux-01 ~]# mysql -uroot -parslinux zabbix < /usr/share/doc/zabbix-server-mysql-4.0.9/create.sql
Warning: Using a password on the command line interface can be ×××ecure.

11、啓動 zabbix-server

[root@arslinux-01 ~]# systemctl start zabbix-server
[root@arslinux-01 ~]# ps aux|grep zabbix

12、zabbix-server 監聽 10051 端口

[root@arslinux-01 ~]# netstat -lntp|grep zabbix
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      7820/zabbix_server
tcp6       0      0 :::10051                :::*                    LISTEN      7820/zabbix_server

13、編輯 zabbix-agent 的配置文件

[root@arslinux-01 ~]# vim /etc/zabbix/zabbix_agentd.conf

Server=127.0.0.1                  //server 去取

ServerActive=127.0.0.1          //機器量多的話,線程就太多,那麼客戶端上報更好

14、啓動 zabbix-agent

[root@arslinux-01 ~]# systemctl start zabbix-agent
[root@arslinux-01 ~]# ps aux|grep zabbix_agent

15、安裝 web ui,需要啓動 httpd 服務,首先關閉 nginx 服務

[root@arslinux-01 ~]# systemctl stop nginx
[root@arslinux-01 ~]# systemctl start httpd
[root@arslinux-01 ~]# netstat -lntp|grep httpd


  • 開始安裝 web ui

1、訪問 http://192.168.194.130/zabbix/setup.php,可以安裝 zabbix 了

7.png

2、在下一步裏,會有紅色的標註 FAIL 的選項,需要修改參數

此處,我們需要找到 php 的配置文件去更改

8.png

3、更改php.ini 參數(httpd 是之前 yum 安裝的) /etc/php.ini 中的 date.timezone

[root@arslinux-01 ~]# vi /etc/php.ini
date.timezone = Asia/Shanghai

4、重啓 httpd

[root@arslinux-01 ~]# systemctl restart httpd

5、到瀏覽器中,刷新一下,可以看到,參數已經全部正常了

9.png

6、定義數據庫相關信息

10.png

7、自定義服務器信息

11.png

12.png

13.png

根據提示,默認安裝完即可

8、到登陸頁面,默認初始用戶名和密碼是 admin 和 zabbix

14.png

9、然後可以到個人管理中更改密碼和語言

15.png

16.png

往後再訪問時就在瀏覽器輸入 ip/zabbix 即可

10、總結:

1)安裝 zabbix yum 源

rpm -ivh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm

2)yum 安裝

yum ×××tall zabbix-agent zabbix-get zabbix-server-mysql zabbix-web zabbix-web-mysql

3)數據庫操作

create database zabbix;

grant all on zabbix.* to 'zabbix'@'127.0.0.1' identified by '密碼'

4)導入數據

/usr/share/doc/zabbix-server-mysql-4.0.3/create.sql.gz

gzid -d /usr/share/doc/zabbix-server-mysql-4.0.3/create.sql.gz

mysql -uroot -p密碼 zabbix < /usr/share/doc/zabbix-server-mysql-4.0.3/create.sql.gz

5)編輯 zabbix_server.conf

vim /etc/zabbix/zabbix_server.conf #定義數據庫相關的參數

systemctl start zabbix-server

systemctl enable zabbix-server

6)編輯 zabbix_agent.conf

vim /etc/zabbix/zabbix_agent.conf #定義Server 和 ServerActive=127.0.0.1

systemctl start zabbix-agent

systemctl enable zabbix-agent

7)httpd

systemctl start httpd #如果已經啓動了 nginx,需要先把 nginx 關閉,然後啓動 httpd;如果 nginx 不能停止,那麼需要更改 nginx 監聽端口爲非 80 端口,比如 8080 /8000

8)web ui 配置

http://ip/zabbix 訪問,解決 php 的問題 (date.timezone = Asia/Shanghai)

/etc/php.ini (httpd 是 yum 安裝)

默認用戶名密碼:admin,zabbix


5.13 監控客戶機-添加主機

1、安裝 zabbix 的 yum 源

[root@arslinux-02 ~]# rpm -ivh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm

2、yum 安裝 zabbix-agent

[root@arslinux-02 ~]# yum ×××tall -y zabbix-agent

3、編輯配置文件 /etc/zabbix/zabbix_agentd.conf

[root@arslinux-02 ~]# vim /etc/zabbix/zabbix_agentd.conf

Server=192.168.194.130 //zabbix-server 的 ip

ServerActive=192.168.194.130

4、啓動 zabbix-agent

[root@arslinux-02 ~]# systemctl start zabbix-agent
[root@arslinux-02 ~]# ps aux|grep zabbix
zabbix     7272  0.0  0.1  80720  1268 ?        S    21:20   0:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbi_agentd.conf
zabbix     7273  0.0  0.1  80720  1312 ?        S    21:20   0:00 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix     7274  0.0  0.1  80720  1836 ?        S    21:20   0:00 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix     7275  0.0  0.1  80720  1836 ?        S    21:20   0:00 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix     7276  0.0  0.1  80720  1836 ?        S    21:20   0:00 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix     7277  0.0  0.2  80852  2224 ?        S    21:20   0:00 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
root       7279  0.0  0.0 112724   988 pts/0    R+   21:20   0:00 grep --color=auto zabbix

5、查看 zabbix-agentd 監聽的端口

[root@arslinux-02 ~]# netstat -lntp|grep zabbix
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      7272/zabbix_agentd
tcp6       0      0 :::10050                :::*                    LISTEN      7272/zabbix_agentd

zabbix-agentd 監聽 10050 端口,會和 server 端的 10051 端口通信


5.14 監控客戶機-管理模板

1、創建新主機

配置——主機——創建主機

21.png

23.png

24.png

主機已經添加完成

2、創建模板

配置——模板——創建模板

25.png

26.png

3、拷貝某個模板裏的應用集 CPU 中的監控項到自定義的模板中

配置——模板——Template OS Linux——應用集——CPU

27.png

4、將監控項拷貝到 test 模板中去

28.png29.png

5、同樣的方法拷貝其他參數的監控項到 test 模板中

30.png

6、自動發現(Discovery)是無法使用複製來添加到自定義模板中的

我們可以將“自動發現”規則一樣的模板克隆到一個新的自定義模板中

配置——模板——模板名稱——全克隆(Full Clone)

31.png

添加名稱和羣組信息

32.png

7、進入新建的模板 ars4life ,在“連接的宏模板”中取消連接,並更新

33.png

8、根據需要刪除模板中的監控項、觸發器、圖形

34.png

刪除順序:圖形——觸發器——監控項——應用集(刪除空白項)

9、將模板鏈接到自定義創建的主機上去

35.png

配置——主機——具體主機——模板——選擇鏈接模板——添加——更新

10、添加完成

36.png

11、查看監控項。監測——最新數據

37.png

12、命令行查看相關信息

[root@arslinux-01 ~]# zabbix_get -s 192.168.194.132 -p 10050 -k "system.cpu.load[all,avg1]"
0.100000


5.15 監控客戶機-管理圖形和窗口

  • 自動發現

自動發現需要一個小時才發現一次,如果需要立刻出現,可以使用現在檢查

主機——自動發現——現在檢查

1.png

2.png

3.png

  • 聚合圖形

監測——聚合圖形——創建聚合圖形

4.png

5.png

6.png

7.png

  • 自定義監測模塊

儀表板中也可以自定義新的監測模塊

創建儀表盤:監測——儀表盤——創建儀表盤

8.png

9.png

效果如下圖

10.png

在保存設置時出錯,是由於名稱不能爲中文,需要改成英文,修改完後正常

11-1.png

在“圖形”以及“聚合圖形”中,將常用的類型圖標收藏,點星星變×××

11.png

在儀表板中的小構件中,可以看到收藏的項目,點進去就能查看圖形

12.png

記得一定要保存設置!!

13.png


5.16 監控客戶機-配置觸發器

  • 創建觸發器

自定義網卡流量觸發器

21.png

觸發器設置:名稱——嚴重性——表達式

表達式(條件):監控項——結果(單位爲 bit)

22.png

23.png

24.png


5.17 解決中文名稱不能寫數據庫的問題

觸發器名稱如果改爲中文,那麼在更新後,會提示錯誤,無法更新觸發器,如下圖

25.png

錯誤原因:數據庫中文出現問題,基本都是字符集的問題

查看數據庫:

mysql> show variables like 'character%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | latin1                           |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | latin1                           |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

character_set_database,需要改爲 utf8 中文

可以先去 /etc/my.cnf 中將上述參數全部改爲 utf8,再導入表,就不會有這種問題了

但在已安裝 zabbix 的情況下,重新安裝 zabbix 就變得極不科學,有什麼更好的辦法呢?

  • 修改字符集

1、將表導出

[root@arslinux-01 ~]# mysqldump -uroot -parslinux --default-character-set=utf8 zabbix > zabbix.sql

2、編輯導出的 .sql 文件

[root@arslinux-01 ~]# vim zabbix.sql

一般模式下輸入 :1,$s/latin1/utf8/g,將latin1全局替換成 utf8,保存退出後再導入數據庫

3、導入數據庫

[root@arslinux-01 ~]# mysql -uroot -parslinux --default-character-set=utf8 zabbix < zabbix.sql

4、再進入數據庫查看默認字符集:

mysql> use zabbix;

mysql> show create table items\G

ENGINE=InnoDB DEFAULT CHARSET=utf8

mysql> show create table triggers\G

ENGINE=InnoDB DEFAULT CHARSET=utf8

5、到 web 中查看名稱是否能更新爲中文

26.png


5.18 解決圖形中文亂碼

27.png

現象:如上圖,亂碼成方塊狀

原因:沒有支持中文文字的字體

解決方法:替換 zabbix 默認字體爲中文字體

步驟:

1、查看 zabbix 配置文件中定義的字體路徑和名稱

vim /usr/share/zabbix/include/defines.inc.php

ZBX_FONTPATH ——> assets/fonts

ZBX_GRAPH_FONT_NAME  ——> graphfont

2、到 /usr/share/zabbix/assets/fonts 裏,將 graphfont.ttf 備份

[root@arslinux-01 ~]# cd /usr/share/zabbix/assets/fonts/
[root@arslinux-01 fonts]# mv graphfont.ttf graphfont.ttf.bak
[root@arslinux-01 fonts]# ls
graphfont.ttf.bak

3、從 windows 主機裏的 C:\Windows\Fonts 上傳一箇中文字體到虛擬機中

4、給改字體做軟鏈接,名稱爲 graphfont.ttf(或者到配置文件中改配置)

[root@arslinux-01 fonts]# ln -s msyh.ttc graphfont.ttf
[root@arslinux-01 fonts]# ls
graphfont.ttf  graphfont.ttf.bak  msyh.ttc

5、web 中亂碼問題已經解決

28.png


5.19~5.21 zabbix郵件告警

  • 設置郵件推送

1、首先新註冊一個域名

2、某雲——產品與服務——雲通信——郵件推送——立即開通

3、郵件推送控制檯——郵件設置——發信域名——新建域名——配置

4、找到雲解析DNS——域名解析——解析設置——添加記錄(根據發信配置的信息填寫,下圖

29.png

30.png

5、修改完成後,點確認修改,再點驗證,狀態會變爲綠色“可使用-未備案”

31.png

6、新建發信地址:郵件設置——發信地址——新建發信地址——發信域名、賬號(自定義)、發信類型爲觸發郵件——設置SMTP密碼(可以用keepass生成隨機字符串,也可以用mkpasswd)

32.png

  • 創建發郵件 python 腳本:

1、編輯 mail.py 腳本

更改mail_user、mail_pass和端口號即可

[root@arslinux-01 ~]# vim mail.py
#!/usr/bin/python
#coding:utf-8

import smtplib
from email.mime.text import MIMEText
import sys

mail_user = '[email protected]'
mail_pass = 'xxxxxxxxx'

def send_mail(to_list,subject,content):
    me = "zabbix 監控告警平臺"+"<"+mail_user+">"
    msg = MIMEText(content, 'plain', 'utf-8')
    msg['Subject'] = subject
    msg['From'] = me
    msg['to'] = to_list

    try:
        s = smtplib.SMTP("smtpdm.aliyun.com", 25)
        s.login(mail_user,mail_pass)
        s.sendmail(me,to_list,msg.as_string())
        s.close()
        return True
    except Exception,e:
        print str(e)
        return False

if __name__ == "__main__":
    send_mail(sys.argv[1], sys.argv[2], sys.argv[3])

2、修改 mail.py 權限爲755

[root@arslinux-01 ~]# chmod 755 mail.py

3、使用命令發一個測試給個人郵箱來測試

[root@arslinux-01 ~]# ./mail.py [email protected] "測試郵件" "看看能否發送成功"

4、進入郵箱查看是否搜到郵件

33.png

5、成功!

  • 設置zabbix_server配置文件

1、在 /etc/zabbix/zabbix_server.conf 找到報警腳本路徑 AlertScriptsPath=/usr/lib/zabbix/alertscripts

2、cd 到 /usr/lib/zabbix/alertscripts

3、把 mail.py 腳本挪到這個目錄下來

[root@arslinux-01 alertscripts]# mv /root/mail.py .

4、發郵件時是 zabbix 用戶執行的,而不是root用戶,確定 mail.py 是755權限

  • 設置zabbixWEB UI

1、設置報警媒介

管理——報警媒介類型——創建媒體類型

34.png

名稱:baojing

類型:腳本

腳本名稱:mail.py(zabbix 會到 zabbix_server 去找 AlertScriptsPath )

添加腳本參數:{ALERT.SENDTO}、{ALERT.SUBJECT}、{ALERT.MESSAGE}

選項保持默認即可,點添加完成

35.png

2、設置接收郵件地址

1)管理——用戶——Admin(或者新創建用戶)

2)Admin——報警媒介——添加

類型:baojing

收件人:郵箱地址

可以添加多個,記得點更新,檢查一下報警媒介是否添加成功

36.png

37.png

3)權限檢查是否所有機器都有讀寫權限

如果沒有讀寫權限,可以更改 “用戶類型” 爲 “超級管理員”

38.png

3、配置動作

1)配置——動作——創建動作

2)“操作”標籤

操作——新的

發送到用戶:Admin(根據實際情況填寫用戶)

僅送到:baojing

條件:事件已確認等於非確認,添加

添加

39.png

40.png

3)“恢復操作”標籤,同上

  • 測試報警

1、配置——主機——觸發器

2、模擬故障:更改一個觸發器,將“網卡入口流量”觸發值改爲 <=10000000

3、監測——問題——查看切換爲問題——選擇主機名——應用

41.png

4、也可以查看郵箱,有郵件告警

42.png

5、將觸發器告警值恢復爲默認,會有恢復操作的郵件通知

43.png


5.22/5.23 zabbix監控Nginx

  • 編寫監控腳本

1、arslinux-02上安裝 nginx 服務

2、配置 Nginx 狀態信息,增加一段內容

[root@arslinux-02 ~]# vim /etc/nginx/nginx.conf
location /nginx_status
{
stub_status on;
access_log   off;
allow 127.0.0.1;
deny all;
}

41.png

如果 include /etc/nginx/default.d/*.conf 之後後沒有看到內容,那麼可能是被單獨定義到了 /etc/nginx/default.d/default.conf 中去,可以將 location /nginx_status 一段內容寫到 default.conf 中去,方法同上

3、測試:

[root@arslinux-02 ~]# curl 127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 0
[root@arslinux-02 ~]# curl 127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
3 3 3
Reading: 0 Writing: 1 Waiting: 0
[root@arslinux-02 ~]# curl 127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
4 4 4
Reading: 0 Writing: 1 Waiting: 0

4、說明:

字段 含義

Active Connections     當前活動連接數,其中也包括了等待狀態的連接

accepts     接收到的連接數

handled     已經處理完的連接數,該數字一般和accepts一致,如果不一致那麼說明Nginx出錯了

requests     總共處理的請求數,一個連接可以有多個請求,所以該值比accpets要大

Reading     正在讀取請求頭信息的連接數

Writing     正在發送響應報文的連接數

Waiting     處於閒置狀態,等待客戶端發送請求的連接數

5、編寫監控腳本

[root@arslinux-02 ~]# vim /usr/local/sbin/ngx_status.sh
#!/bin/bash
url="http://127.0.0.1/nginx_status"
curl=/usr/bin/curl
# 檢測nginx進程是否存在
function ping {
    /sbin/pidof nginx | wc -l
}
# 檢測nginx性能
function active {
$curl $url 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
$curl $url 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
    $curl $url 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
    $curl $url 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
    $curl $url 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
    $curl $url 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
    $curl $url 2>/dev/null| awk NR==3 | awk '{print $3}'
}
$1

6、更改腳本權限

[root@arslinux-02 ~]# chmod 755 /usr/local/sbin/ngx_status.sh

7、執行腳本,如果數值不爲 0,那麼 nginx 就在運行

[root@arslinux-02 ~]# /usr/local/sbin/ngx_status.sh ping
1
  • 編輯 zabbix_agent.conf

1、在 UserParameter= 下增加一行

UserParameter=nginx.status[*],/usr/local/sbin/ngx_status.sh $1

用來定義自定義的監控腳本,多個腳本定義多行

2、指定 key 和 shell 腳本的路徑名稱

nginx.status[*] 就是 key,[*] 表示腳本帶有參數

3、重啓服務

[root@arslinux-02 ~]# systemctl restart zabbix-agent

4、在 zabbix-server 上測試

[root@arslinux-01 ~]# zabbix_get -s 192.168.194.132 -k 'nginx.status[accepts]'
6
[root@arslinux-01 ~]# zabbix_get -s 192.168.194.132 -k 'nginx.status[ping]'
1

nginx 只要是啓動狀態,必然有數值

  • 添加進 web 界面

1、虛擬機安裝 git

[root@arslinux-02 ~]# yum ×××tall -y git

2、將內容全部 clone 到 虛擬機上

[root@arslinux-02 ~]# git clone https://github.com/aminglinux/linux2019.git

3、將模板載到 windows 端

[root@arslinux-02 ~]# cd zabbix_nginx_template
[root@arslinux-02 zabbix_nginx_template]# sz zbx_export_templates.xml

4、web 上:刪除重名模板 Template App NGINX

5、配置——模板——導入——導入文件(選擇文件,選 zbx_export_templates.xml 文件)

  • 鏈接模板

1、配置——主機——主機名稱——模板——鏈接指示器(選擇Template App NGINX)——添加——更新

2、監控項、圖形裏已經出現了 Nginx 相關的監控


錯誤解決:

1、無效的參數 "/source": UTF-8中無效的字節排序 。

導入的模板不可以直接複製源碼,重命名記事本文件


5.24 zabbix監控Tomcat

監控流程:

1、安裝zabbix_java_gateway

2、配置zabbix_java_gateway配置文件和zabbix_server配置文件,指定監聽端口,啓動進程數

3、編輯Tomcat配置文件,開啓JMX

4、添加host,指定JMX interface

5、鏈接TOMCAT模板


  • 操作步驟:

1、在 zabbix-server 上安裝 zabbix-java-gateway

[root@arslinux-01 ~]# yum ×××tall -y zabbix-java-gateway

2、編輯 zabbix_java_gateway.conf

[root@arslinux-01 ~]# vim /etc/zabbix/zabbix_java_gateway.conf

LISTEN_PORT=10052 //不打開也是默認 10052 端口

START_POLLERS=5 //進程數

3、編輯 /etc/zabbix/zabbix_server.conf

[root@arslinux-01 ~]# vim /etc/zabbix/zabbix_server.conf

JavaGateway=127.0.0.1 //JavaGateway 所安裝的主機 ip,我們這裏是本機

JavaGatewayPort=10052 //java 的 port

StartJavaPollers=5

4、啓動 zabbix-java-gateway

[root@arslinux-01 ~]# systemctl start zabbix-java-gateway

5、重啓 zabbix-server 服務

[root@arslinux-01 ~]# systemctl restart zabbix-server

6、查看進程和端口

[root@arslinux-01 ~]# ps aux|grep gateway
zabbix     8023  1.0  3.6 2267640 36624 ?       Sl   20:28   0:00 java -server -Dlogback.configurationFile=/etc/zabbix/zabbix_java_gateway_logback.xml -classpath lib:lib/android-json-4.3_r3.1.jar:lib/logback-classic-0.9.27.jar:lib/logback-core-0.9.27.jar:lib/slf4j-api-1.6.1.jar:bin/zabbix-java-gateway-4.0.9.jar -Dzabbix.pidFile=/var/run/zabbix/zabbix_java.pid -Dzabbix.listenPort=10052 -Dzabbix.startPollers=5 -Dsun.rmi.transport.tcp.responseTimeout=3000 com.zabbix.gateway.JavaGateway
root       8108  0.0  0.0 112728   988 pts/0    S+   20:29   0:00 grep --color=auto gateway
[root@arslinux-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      7223/rpc.mountd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7156/sshd
tcp        0      0 0.0.0.0:36919           0.0.0.0:*               LISTEN      7171/rpc.statd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7572/master
tcp        0      0 0.0.0.0:43225           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      7196/zabbix_agentd
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      8049/zabbix_server
tcp6       0      0 :::41833                :::*                    LISTEN      -
tcp6       0      0 :::3306                 :::*                    LISTEN      7479/mysqld
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::80                   :::*                    LISTEN      7155/httpd
tcp6       0      0 :::20048                :::*                    LISTEN      7223/rpc.mountd
tcp6       0      0 :::22                   :::*                    LISTEN      7156/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      7572/master
tcp6       0      0 :::2049                 :::*                    LISTEN      -
tcp6       0      0 :::10050                :::*                    LISTEN      7196/zabbix_agentd
tcp6       0      0 :::10051                :::*                    LISTEN      8049/zabbix_server
tcp6       0      0 :::43939                :::*                    LISTEN      7171/rpc.statd
tcp6       0      0 :::10052                :::*                    LISTEN      8023/java

7、開啓 JMX

1)編輯配置文件 /usr/local/tomcat/bin/catalina.sh

在 #!/bin/sh 下增加一段

export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote

-Djava.rmi.server.hostname=192.168.194.130

-Dcom.sun.management.jmxremote.port=9999

-Dcom.sun.management.jmxremote.ssl=false

-Dcom.sun.management.jmxremote.authenticate=false"

2)重啓 tomcat

[root@arslinux-01 ~]# /usr/local/tomcat/bin/shutdown.sh
[root@arslinux-01 ~]# /usr/local/tomcat/bin/startup.sh

3)查看 9999 端口是否啓動

[root@arslinux-01 ~]# netstat -lntp |grep 9999
tcp6       0      0 :::9999                 :::*                    LISTEN      8328/java

8、zabbix web 端添加新 host

1)創建新主機,配置——主機——創建主機

2)更改主機配置:主機名稱——羣組——移除agent代理程序接口——添加JMX接口——127.0.0.1,端口 9999

1.png

3)鏈接模板:模板——Template App Apache Tomcat JMX——添加——添加

2.png

4)監測——最新數據——主機切換爲arslinux-01_tomcat已經出數據了


5.25 zabbix監控MySQL

1、授權用戶

[root@arslinux-01 ~]# mysql -uroot -parslinux
mysql> grant usage,process,replication client on*.* to 'mon'@'localhost' identified by '12345678';
Query OK, 0 rows affected (0.05 sec)

2、查看 /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf 中 HOME 的位置

HOME=/var/lib/zabbix

3、創建目錄 /var/lib/zabbix

[root@arslinux-01 ~]# mkdir /var/lib/zabbix

4、在目錄下創建隱藏文件 .my.cnf,添加兩段內容

[root@arslinux-01 ~]# vim /var/lib/zabbix/.my.cnf
[mysql]
host=localhost
user=mon
password='12345678'
socket=/tmp/mysql.sock

[mysqladmin]
host=localhost
user=mon
password='12345678'
socket=/tmp/mysql.sock

5、重啓 zabbix-agent 服務(如果沒改 zabbix 配置文件無需重啓)

[root@arslinux-01 ~]# systemctl restart zabbix-agent

6、測試

[root@arslinux-01 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k mysql.ping
sh: mysqladmin: 未找到命令
0

7、mysqladmin 的位置

[root@arslinux-01 ~]# which mysqladmin
/usr/local/mysql/bin/mysqladmin

由於 PATH 的原因,可以給 mysqladmin 做個軟連接到 /usr/bin/ 下

8、做軟鏈接

[root@arslinux-01 ~]# ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/

9、再次測試

[root@arslinux-01 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k mysql.ping
1

10、測試下 mysql.version、mysql.size

[root@arslinux-01 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k mysql.version

sh: mysql: 未找到命令

[root@arslinux-01 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k mysql.size

bash: mysql: 未找到命令

11、通方法給 mysql 做軟鏈接

[root@arslinux-01 ~]# which mysql
/usr/local/mysql/bin/mysql
[root@arslinux-01 ~]# ln -s /usr/local/mysql/bin/mysql /usr/bin/
[root@arslinux-01 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k mysql.version
mysql  Ver 14.14 Distrib 5.6.43, for linux-glibc2.12 (x86_64) using  EditLine wrapper
[root@arslinux-01 ~]# zabbix_get -s 127.0.0.1 -p 10050 -k mysql.size
10240

11、添加主機和模板

1)配置——主機——創建主機——主機名稱——羣組——端口

3.png

2)模板——Template DB MySQL——添加

4.png

3)監測——最新數據——主機切換爲 arslinux-01_mysql ,過段時間會出現新數據

4)主機可用性也顯示 ZBX 可用

5.png



課堂筆記

1、監控重要性

2、主流監控:zabbix、open-falcon、Prometheus、grafana

鏈路監控:apm、pinport

3、學習監控需要把握的幾點:監控項目如何配置、自定義監控如何配置、如何成圖、如何告警


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章