[Up]常用資訊

[重點文章] 重點文章 [重點文章] 重點文章

2020年5月27日 星期三

[Debian] 使用 Telegraf, InfluxDB and Grafana 監控網路狀態

[Debian] 使用 Telegraf, InfluxDB and Grafana 監控網路狀態

 
#Step: 01 – Grafana 安裝在 Debian 10 上
說明


apt update

安裝資料庫

apt install mariadb-server

修改 root 使用密碼登入方式

USE mysql;
UPDATE user SET plugin='mysql_native_password' WHERE user='root';
FLUSH PRIVILEGES;
exit;
	
mysql_secure_installation

#
CREATE DATABASE grafana CHARACTER SET UTF8 COLLATE UTF8_BIN;

CREATE USER 'grafana'@'%' IDENTIFIED BY 'Oscar123wu123';

GRANT ALL PRIVILEGES ON grafana.* TO 'grafana'@'%';
quit;

#
sudo apt-get install -y apt-transport-https
sudo apt-get install -y software-properties-common wget
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -

# Alternatively you can add the beta repository, see in the table above
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"

sudo apt-get update
sudo apt-get install grafana

OR 
sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/oss/release/grafana_7.0.1_amd64.deb
sudo dpkg -i grafana_7.0.1_amd64.deb
#

vi /etc/grafana/grafana.ini

=====================================================================
[database]
type = mysql
host = 127.0.0.1:3306
name = grafana
user = grafana
password = Oscar123wu123

[session]
provider = mysql
provider_config = `grafana:Oscar123wu123@tcp(127.0.0.1:3306)/grafana`
=====================================================================

service grafana-server start

systemctl enable --now grafana-server

systemctl status grafana-server

#
 
#Step: 02 – influxdb 安裝在 Debian 10 上
說明


mkdir /downloads
cd /downloads
wget https://dl.influxdata.com/influxdb/releases/influxdb_1.8.0_amd64.deb
dpkg -i influxdb_1.8.0_amd64.deb

systemctl enable --now influxdb
systemctl status influxdb
 
#Step: 03 – telegraf 安裝在 Debian 10 上
說明

mkdir /downloads
cd /downloads
wget https://dl.influxdata.com/telegraf/releases/telegraf_1.14.3-1_amd64.deb
dpkg -i telegraf_1.14.3-1_amd64.deb

systemctl enable --now telegraf

確認服務
systemctl status telegraf


設定基本設定
vi /etc/telegraf/telegraf.conf
================================================
[global_tags]
[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  hostname = ""
  omit_hostname = false
[[outputs.influxdb]]
[[inputs.cpu]]
  percpu = true
  totalcpu = true
  collect_cpu_time = false
  report_active = false
[[inputs.disk]]
  ignore_fs = ["tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs"]
[[inputs.diskio]]
[[inputs.kernel]]
[[inputs.mem]]
[[inputs.processes]]
[[inputs.swap]]
[[inputs.system]]

================================================
設定監控主機
vi /etc/telegraf/telegraf.d/monitor-linux-snmp.conf
================================================
[[inputs.snmp]]
  agents = [ "172.31.41.29:161"]
  version = 2
  community = "GokuBlack"
  name = "snmp"

 [[inputs.snmp.field]]
    name = "hostname"
    oid = "RFC1213-MIB::sysName.0"
    is_tag = true

  [[inputs.snmp.table]]
    name = "snmp"
    inherit_tags = [ "hostname" ]
    oid = "IF-MIB::ifXTable"

    [[inputs.snmp.table.field]]
      name = "ifName"
      oid = "IF-MIB::ifName"
      is_tag = true
	  
=====================================================

重啟 telegraf 

service telegraf restart

##

1. 安裝 snmpd 服務
# apt-get install snmpd snmp

2.  修改 /etc/snmp/snmpd.conf 設定檔
# sed -i 's/#rocommunity public  localhost/rocommunity public  localhost/' /etc/snmp/snmpd.conf

3. 讓其它的 IP 可以讀取 snmp 相關資訊
# sed -i 's/127.0.0.1/0.0.0.0/' /etc/snmp/snmpd.conf
或
# sed -i 's/agentAddress  udp:127.0.0.1:161/#agentAddress  udp:127.0.0.1:161/' /etc/snmp/snmpd.conf
# sed -i 's/#agentAddress udp:161/agentAddress udp:161/' /etc/snmp/snmpd.conf

或
# echo 'rocommunity public' > /etc/snmp/snmpd.conf
# chmod 600 /etc/snmp/snmpd.conf

4. 啟動 snmpd 服務
# /etc/init.d/snmpd start
Starting network management services: snmpd.

5. 檢查 snmpd 是否有正常啟動
# netstat -anulp | grep 161
udp        0      0 0.0.0.0:161           0.0.0.0:*                           4540/snmpd

6. 讀取 snmpd 資訊
# snmpwalk -v 1 -c public localhost | less
# snmpwalk -v 2c -c public localhost | less

7. 在防火牆設定限制
# iptabels -A INPUT -u udp -s x.x.x.x --dport 161 -m state --state NEW -j ACCEPT

調整吐出來的資訊完整度 , default 可改成 host ip (正面表列)

rocommunity修改成如下
#rocommunity6 public  default   -V systemonly
#rocommunity public  default    -V systemonly
rocommunity public default

service snmpd restart


設定主機
 
#Step: 04 – telegraf 設定檔
說明

vi /etc/telegraf/telegraf.conf

[global_tags]
[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  hostname = ""
  omit_hostname = false
[[outputs.influxdb]]
[[inputs.cpu]]
  percpu = true
  totalcpu = true
  collect_cpu_time = false
  report_active = false
[[inputs.disk]]
  ignore_fs = ["tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs"]
[[inputs.diskio]]
[[inputs.kernel]]
[[inputs.mem]]
[[inputs.processes]]
[[inputs.swap]]
[[inputs.system]]

vi /etc/telegraf/telegraf.d/monitor-linux-snmp.conf

[[inputs.snmp]]
  agents = [ "127.0.0.1:161"]
  version = 2
  community = "public"
  name = "snmp"

 [[inputs.snmp.field]]
    name = "hostname"
    oid = "RFC1213-MIB::sysName.0"
    is_tag = true

  [[inputs.snmp.table]]
    name = "snmp"
    inherit_tags = [ "hostname" ]
    oid = "IF-MIB::ifXTable"

    [[inputs.snmp.table.field]]
      name = "ifName"
      oid = "IF-MIB::ifName"
      is_tag = true
	  
=========================

service telegraf restart

telegraf -config /etc/telegraf/telegraf.d/monitor-linux-snmp.conf --test

influx

> use telegraf
Using database telegraf
> show measurements
name: measurements
name
----
cpu
disk
diskio
interface
kernel
mem
processes
snmp
swap
system


> select * from snmp limit 2
name: snmp
time                agent_host host                                    hostname                                ifConnectorPresent ifCounterDiscontinuityTime ifHCInBroadcastPkts ifHCInMulticastPkts ifHCInOctets ifHCInUcastPkts ifHCOutBroadcastPkts ifHCOutMulticastPkts ifHCOutOctets ifHCOutUcastPkts ifHighSpeed ifInBroadcastPkts ifInMulticastPkts ifName ifOutBroadcastPkts ifOutMulticastPkts ifPromiscuousMode
----                ---------- ----                                    --------                                ------------------ -------------------------- ------------------- ------------------- ------------ --------------- -------------------- -------------------- ------------- ---------------- ----------- ----------------- ----------------- ------ ------------------ ------------------ -----------------
1590563960000000000 127.0.0.1  Debian10-4-InfluxDB-Telegraf-Grafana-02 Debian10-4-InfluxDB-Telegraf-Grafana-02 1                  0                          0                   0                   163832728    117733          0                    0                    14911617      57903            10000       0                 0                 ens192 0                  0                  2
1590563960000000000 127.0.0.1  Debian10-4-InfluxDB-Telegraf-Grafana-02 Debian10-4-InfluxDB-Telegraf-Grafana-02 2                  0                          0                   0                   6058851      35460           0                    0                    6058851       35460            10          0                 0                 lo     0     
 
#Step: 05 – Grafana 監控SNMP 設備
說明

Grafana Monitoring SNMP Device 

要修改相對應的主機名稱和網卡

SELECT non_negative_derivative(mean("ifHCInOctets"), 1s) *8 AS "In" FROM "snmp" WHERE ("hostname" = 'Debian10-4-InfluxDB-Telegraf-Grafana-02' AND "ifName" = 'ens192') AND $timeFilter GROUP BY time($__interval), "hostname", "ifName" fill(null)

SELECT non_negative_derivative(mean("ifHCOutOctets"), 1s) *8 AS "Out" FROM "snmp" WHERE ("hostname" = 'Debian10-4-InfluxDB-Telegraf-Grafana-02' AND "ifName" = 'ens192') AND $timeFilter GROUP BY time($__interval), "hostname", "ifName" fill(null)

 
#Step: 05 – Grafana 不同監控SNMP 設備 到不同influxdb
說明

當前3種最佳解決方法是:

使用 name_override,name_suffix 或 name_prefix 配置選項將名稱放在輸入端的測量之前,然後使用namepass路由到輸出,範例如下:

這邊使用 name_prefix 方式施作
=============================================================
[[inputs.exec]]
  # ...
  name_prefix = "exec1_"

[[inputs.exec]]
  # ...
  name_prefix = "exec2_"

# this influxdb will only get exec1
[[outputs.influxdb]]
  # ...
  namepass = "exec1_*"

# this will only get exec2
[[outputs.influxdb]]
  # ...
  namepass = "exec2_*"

# 輸入如下:

[[inputs.snmp]]
  # ...
  name_prefix = "exec1_"
  agents = [ "192.168.1.250:161" ]
  version = 2
  community = "public"
  name = "snmp"

 [[inputs.snmp.field]]
    name = "hostname"
    oid = "RFC1213-MIB::sysName.0"
    is_tag = true

  [[inputs.snmp.table]]
    name = "snmp"
    inherit_tags = [ "hostname" ]
    oid = "IF-MIB::ifXTable"

    [[inputs.snmp.table.field]]
      name = "ifName"
      oid = "IF-MIB::ifName"
      is_tag = true

[[inputs.snmp]]
  # ...
  name_prefix = "exec2_"
  agents = [ "192.168.10.100:161" ]
  version = 2
  community = "load_public"
  name = "snmp"

 [[inputs.snmp.field]]
    name = "hostname"
    oid = "RFC1213-MIB::sysName.0"
    is_tag = true

  [[inputs.snmp.table]]
    name = "snmp"
    inherit_tags = [ "hostname" ]
    oid = "IF-MIB::ifXTable"

    [[inputs.snmp.table.field]]
      name = "ifName"
      oid = "IF-MIB::ifName"
      is_tag = true


輸出如下:

[outputs]
[outputs.influxdb]
    #.....
	namepass = ["exec1_*"]
    # The full HTTP endpoint URL for your InfluxDB instance
    url = "http://localhost:8086"
    # The target database for metrics. Telegraf will create the DB 1 if it does not exist
    database = "exec1_telegraf"

[outputs]
[outputs.influxdb]
    #.....
	namepass = ["exec2_*"]
    # The full HTTP endpoint URL for your InfluxDB instance
    url = "http://localhost:8086"
    # The target database for metrics. Telegraf will create the DB 2 if it does not exist
    database = "exec2_telegraf"

==========================================================================
可以寫入道不同資料庫方式 要注意 name 會改變 變成 exec3_snmp 顯示如下
####################################
> show measurements
name: measurements
name
----
exec3_snmp

select * from exec3_snmp limit 2
####################################
[[inputs.snmp]]
  name_prefix = "exec3_"
  agents = ["xxx.xxx.xxx.xxx"]
  version = 2
  community = "public"
  interval = "60s"
  timeout = "10s"
  retries = 3
  name = "snmp"

 [[inputs.snmp.field]]
  name = "hostname"
  oid = "RFC1213-MIB::sysName.0"
  is_tag = true

 [[inputs.snmp.field]]
  name = "pool_client_connections"
  oid = "F5-BIGIP-LOCAL-MIB::ltmPoolStatServerCurConns.15.47.67.111.109.109.111.110.47.71.87.95.112.111.111.108"

 [[inputs.snmp.field]]
  name = "F5_client_connections"
  oid = "F5-BIGIP-SYSTEM-MIB::sysStatClientCurConns.0"

[outputs]
[outputs.influxdb]
    #.....
    namepass = ["exec3_*"]
    # The full HTTP endpoint URL for your InfluxDB instance
    url = "http://localhost:8086"
    # The target database for metrics. Telegraf will create the DB 3 if it does not exist
    database = "exec3_telegraf"

==========================================================================

使用標籤,標籤放置,標籤傳遞路由
運行多個Telegraf進程。
##

#配合ID 928  Templated dashboard for telegraf + influxdb. 設定檔

# Global tags can be specified here in key="value" format.
[global_tags]
  # dc = "us-east-1" # will tag all metrics with dc=us-east-1
  # rack = "1a"
  ## Environment variables can be used as tags, and throughout the config file
  # user = "$USER"


# Configuration for telegraf agent
[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  debug = false
  quiet = false
  hostname = ""
  omit_hostname = false


###############################################################################
#                                  OUTPUTS                                    #
###############################################################################

# Configuration for influxdb server to send metrics to 
[[outputs.influxdb]]
  #urls = ["http://your_host:8086"]
  urls = ["http://localhost:8086"]
  database = "telegraf_metrics"

  ## Retention policy to write to. Empty string writes to the default rp.
  retention_policy = ""
  ## Write consistency (clusters only), can be: "any", "one", "quorum", "all"
  write_consistency = "any"

  ## Write timeout (for the InfluxDB client), formatted as a string.
  ## If not provided, will default to 5s. 0s means no timeout (not recommended).
  timeout = "5s"
  # username = "telegraf"
  # password = "2bmpiIeSWd63a7ew"
  ## Set the user agent for HTTP POSTs (can be useful for log differentiation)
  # user_agent = "telegraf"
  ## Set UDP payload size, defaults to InfluxDB UDP Client default (512 bytes)
  # udp_payload = 512

###############################################################################
#                                  INPUTS                                     #
###############################################################################

# Read metrics about cpu usage
[[inputs.cpu]]
  ## Whether to report per-cpu stats or not
  percpu = true
  ## Whether to report total system cpu stats or not
  totalcpu = true
  ## Comment this line if you want the raw CPU time metrics
  fielddrop = ["time_*"]


# Read metrics about disk usage by mount point
[[inputs.disk]]
  ## By default, telegraf gather stats for all mountpoints.
  ## Setting mountpoints will restrict the stats to the specified mountpoints.
  # mount_points = ["/"]

  ## Ignore some mountpoints by filesystem type. For example (dev)tmpfs (usually
  ## present on /run, /var/run, /dev/shm or /dev).
  ignore_fs = ["tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs"]


# Read metrics about disk IO by device
[[inputs.diskio]]
  ## By default, telegraf will gather stats for all devices including
  ## disk partitions.
  ## Setting devices will restrict the stats to the specified devices.
  # devices = ["sda", "sdb"]
  ## Uncomment the following line if you need disk serial numbers.
  # skip_serial_number = false

[[inputs.io]]
# Get kernel statistics from /proc/stat
[[inputs.kernel]]
  # no configuration


# Read metrics about memory usage
[[inputs.mem]]
  # no configuration


# Get the number of processes and group them by status
[[inputs.processes]]
  # no configuration


# Read metrics about swap memory usage
[[inputs.swap]]
  # no configuration


# Read metrics about system load & uptime
[[inputs.system]]
  # no configuration

# Read metrics about network interface usage
[[inputs.net]]
  # collect data only about specific interfaces
  # interfaces = ["eth0"]


[[inputs.netstat]]
  # no configuration

[[inputs.interrupts]]
  # no configuration

[[inputs.linux_sysctl_fs]]
  # no configuration
  
########################

沒有留言:

張貼留言