[Up]常用資訊

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

2021年8月31日 星期二

[Debian] Debian 11.0 (bullseye) 安裝 新版 普羅米修斯(prometheus) + Grafana 8.1.2

[Debian] Debian 11.0 (bullseye) 安裝 新版 普羅米修斯(prometheus) + Grafana

[Debian] Debian 11.0 (bullseye) 安裝 新版 普羅米修斯(prometheus) + node_exporter + Grafana 8.1.2

 
#Step 01 – 基本安裝


Step A : Create Prometheus system user / group

sudo groupadd --system prometheus
sudo useradd -s /sbin/nologin --system -g prometheus prometheus

Step B: Create configuration and data directories

sudo mkdir /var/lib/prometheus
for i in rules rules.d files_sd; do sudo mkdir -p /etc/prometheus/${i}; done

Step C: Download and Install Prometheus on Debian 11 (bullseye)

# 一般來說 wget 已經安裝可省略
# sudo apt-get -y install wget
mkdir -p /tmp/prometheus && cd /tmp/prometheus
curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest \
  | grep browser_download_url \
  | grep linux-amd64 \
  | cut -d '"' -f 4 \
  | wget -qi -

# 解壓縮檔案

tar xvf prometheus*.tar.gz
cd prometheus*/

sudo mv prometheus promtool /usr/local/bin/

sudo mv prometheus.yml  /etc/prometheus/prometheus.yml

sudo mv consoles/ console_libraries/ /etc/prometheus/
cd ~/
rm -rf /tmp/prometheus

sudo vi /etc/prometheus/prometheus.yml

#############################################################################
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']
#############################################################################

Step D: Create a Prometheus systemd Service unit file

##########################################################
sudo tee /etc/systemd/system/prometheus.service<>

for i in rules rules.d files_sd; do sudo chown -R prometheus:prometheus /etc/prometheus/${i}; done
for i in rules rules.d files_sd; do sudo chmod -R 775 /etc/prometheus/${i}; done
sudo chown -R prometheus:prometheus /var/lib/prometheus/

重啟服務
sudo systemctl daemon-reload
sudo systemctl start prometheus
sudo systemctl enable prometheus

$  systemctl status prometheus

http://[ip_hostname]:9090.

Step E: Install node_exporter on Debian 10 Buster

curl -s https://api.github.com/repos/prometheus/node_exporter/releases/latest \
| grep browser_download_url \
| grep linux-amd64 \
| cut -d '"' -f 4 \
| wget -qi -

tar -xvf node_exporter*.tar.gz
cd  node_exporter*/
sudo cp node_exporter /usr/local/bin
# 確認版本
$ node_exporter --version

設定啟動服務

sudo tee /etc/systemd/system/node_exporter.service <>

sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter

$  systemctl status node_exporter.service 

#將服務監控加入普羅米修斯
sudo vi /etc/prometheus/prometheus.yml

- job_name: 'node_exporter'
  static_configs:
  - targets: ['localhost:9100']

sudo systemctl restart prometheus

################################
# 安裝 SNMP_exporter
###############################

curl -s https://api.github.com/repos/prometheus/snmp_exporter/releases/latest \
| grep browser_download_url \
| grep linux-amd64 \
| cut -d '"' -f 4 \
| wget -qi -


tar -xvf snmp_exporter*.tar.gz
cd  snmp_exporter*/
sudo cp snmp_exporter /usr/local/bin
sudo cp ./snmp.yml /usr/local/bin/snmp.yml

$ snmp_exporter --version 

sudo tee /etc/systemd/system/snmp-exporter.service <>

systemctl daemon-reload
sudo systemctl start snmp-exporter
sudo systemctl enable snmp-exporter

sudo systemctl status snmp-exporter

# 新增 到 prometheus 設定當中

sudo vi /etc/prometheus/prometheus.yml

...
  - job_name: snmp
    metrics_path: /snmp
    params:
      module: [if_mib]
    static_configs:
      - targets:
        - 127.0.0.1
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9116  # URL as shown on the UI



promtool check config /etc/prometheus/prometheus.yml

sudo systemctl restart prometheus

 
#Step 02 – 安裝Grafana


#$ echo deb http://nginx.org/packages/debian/ stretch nginx | sudo tee /etc/apt/sources.list.d/nginx.list
$ echo "deb http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
$ wget http://nginx.org/keys/nginx_signing.key && sudo apt-key add nginx_signing.key 
$ sudo apt update && apt install nginx -y

需要注意的是,這一步安裝的 Nginx 和系統自帶的 nginx 的配置目錄略有區別,可以用一下幾個簡單的命令修正:
讓設定習慣不用改變
sudo mkdir /etc/nginx/{sites-available,sites-enabled}
sudo mv /etc/nginx/conf.d/* /etc/nginx/sites-available
sudo rmdir -f /etc/nginx/conf.d/
sudo perl -pi -e 's/conf.d/sites-enabled/g' /etc/nginx/nginx.conf

要設定一下設定檔連結
ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/

mkdir -p /var/www/html

chown -R nginx:nginx /var/www/html

檢查 nginx 設定檔是否正確

nginx -t

重新啟動 nginx 並 設定開機啟動

systemctl restart nginx 
systemctl enable nginx 

 
#Step 03 – 安裝Grafana 8.1.2


sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/oss/release/grafana_8.1.2_amd64.deb
sudo dpkg -i grafana_8.1.2_amd64.deb


systemctl daemon-reload
systemctl start grafana-server
systemctl enable grafana-server.service

安裝套件
grafana-cli plugins install grafana-piechart-panel

grafana-cli plugins install grafana-worldmap-panel

grafana-cli plugins install natel-discrete-panel

grafana-cli plugins install grafana-image-renderer

grafana-cli plugins install flant-statusmap-panel

grafana-cli plugins install grafana-clock-panel

grafana-cli plugins install cloudflare-app

grafana-cli plugins install jdbranham-diagram-panel

vi /etc/grafana/grafana.ini

#  修改
# The http port  to use
;http_port = 3000

# The public facing domain name used to access grafana from a browser與送出連結有關
;domain = localhost
;domain = xxx.xxx.xxx.xxx

# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
;enforce_domain = false

# The full public facing url you use in browser, used for redirects and emails
# If you use reverse proxy and sub path specify full url (with sub path)
;root_url = %(protocol)s://%(domain)s:%(http_port)s/
root_url = %(protocol)s://%(domain)s/grafana/

systemctl start grafana-server

##### 將sqlite3 換成 mariadb mysql  
create database argus_grafana;
GRANT USAGE ON `argus_grafana`.* to 'grafana'@'127.0.0.1' identified by 'gIWeWCa2k8GuMJSM61';
GRANT ALL PRIVILEGES ON `argus_grafana`.* to 'grafana'@'127.0.0.1' with grant option;
flush privileges;

#################################### Database ####################################
[database]
# You can configure the database connection by specifying type, host, name, user and password
# as separate properties or as on string using the url properties.

# Either "mysql", "postgres" or "sqlite3", it's your choice
;type = sqlite3
;host = 127.0.0.1:3306
;name = grafana
;user = root
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
;password =
type = mysql
host = 127.0.0.1:3306
name = argus_grafana
user = grafana
password = gIWeWCa2k8GuMJSM61
#url = mysql://grafana:gIWeWCa2k8GuMJSM61@127.0.0.1:3306/argus_grafana



 
#Step 04 –


安裝 

mysql -u root -p

CREATE DATABASE kanboard CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL PRIVILEGES ON kanboard.* TO 'kanboard'@'localhost' IDENTIFIED BY 'mypassword';
FLUSH PRIVILEGES;
\q

wget https://github.com/kanboard/kanboard/archive/v1.2.10.tar.gz


wget https://github.com/kanboard/kanboard/archive/refs/tags/v1.2.20.tar.gz

tar -xvf v1.2.20.tar.gz

cp -r kanboard-1.2.20 /var/www/html/kanboard

cd /var/www/html/kanboard
cp config.default.php config.php

vi config.php


// Database driver: sqlite, mysql or postgres (sqlite by default)
define('DB_DRIVER', 'mysql');

// Mysql/Postgres username
define('DB_USERNAME', 'kanboard');

// Mysql/Postgres password
define('DB_PASSWORD', 'password');

// Mysql/Postgres hostname
define('DB_HOSTNAME', 'localhost');

// Mysql/Postgres database name
define('DB_NAME', 'kanboard');

chown -R nginx:nginx /var/www/html/kanboard


###############
server {
        listen       80;
        server_name  example.com;
        index        index.php;
        root         /var/www/html/kanboard;
        client_max_body_size 32M;

        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_index index.php;
            include fastcgi_params;
        }

#        location ~* ^.+\.(log|sqlite)$ {
#            return 404;
#        }
#
#        location ~ /\.ht {
#            return 404;
#        }
#
#        location ~* ^.+\.(ico|jpg|gif|png|css|js|svg|eot|ttf|woff|woff2|otf)$ {
#            log_not_found off;
#            expires 7d;
#            etag on;
#        }
#
#        gzip on;
#        gzip_comp_level 3;
#        gzip_disable "msie6";
#        gzip_vary on;
#        gzip_types
#            text/javascript
#            application/javascript
#            application/json
#            text/xml
#            application/xml
#            application/rss+xml
#            text/css
#            text/plain;
    }


systemctl restart php8.0-fpm.service

Plugins
Plugin folder:

define('PLUGINS_DIR', 'data/plugins');
Enable/disable plugin installation from the user interface:

開啟插件
define('PLUGIN_INSTALLER', false); // Default is false since Kanboard v1.2.8
Change default plugin directory URL:

define('PLUGIN_API_URL', 'https://kanboard.org/plugins.json');


 
#Step 05 –


 
#Step 06 –


 
#Step 07 –


 
#Step 08 –


 
#Step 09 –


 
#Step 10 – 備用安裝


useradd -M -r -s /bin/false prometheus

mkdir /etc/prometheus /var/lib/prometheus

指定版本
VER=2.26.0

wget https://github.com/prometheus/prometheus/releases/download/v$VER/prometheus-$VER.linux-amd64.tar.gz


tar xzf prometheus-$VER.linux-amd64.tar.gz

cp prometheus-$VER.linux-amd64/{prometheus,promtool} /usr/local/bin/

chown prometheus:prometheus /usr/local/bin/{prometheus,promtool}

cp -r prometheus-$VER.linux-amd64/{consoles,console_libraries} /etc/prometheus/


cp prometheus-$VER.linux-amd64/prometheus.yml /etc/prometheus/

less /etc/prometheus/prometheus.yml

chown -R prometheus:prometheus /etc/prometheus

chown -R prometheus:prometheus /var/lib/prometheus

prometheus --config.file=/etc/prometheus/prometheus.yml


cat > /etc/systemd/system/prometheus.service << 'EOL'
[Unit]
Description=Prometheus Time Series Collection and Processing Server
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target
EOL

systemctl daemon-reload

systemctl enable --now  prometheus

systemctl status prometheus

沒有留言:

張貼留言