[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
ラベル Prometheus の投稿を表示しています。 すべての投稿を表示
ラベル Prometheus の投稿を表示しています。 すべての投稿を表示

2018年9月17日月曜日

AnsibleでRaspberry PiにPrometheus Node Exporterをインストールする

Prometheus Node Exporterでハードウェア、OS情報の収集を行います。

〇インストール方法
1.下準備
~/.ansible.cfgに以下の内容を設定します
[ssh_connection]
pipelining=True
[defaults]
host_key_checking = False

2. Prometheus Node Exporterをインストール対象のユーザやパスワードを環境に合わせてinventoryファイルに記入します

inventory例
[node-exporter]
192.168.1.16

[all:vars]
ansible_ssh_port=22
ansible_ssh_user=pi
ansible_ssh_pass=raspberry
ansible_sudo_pass=raspberry

3.inventory node-exporter-deb.ymlを準備し、以下のコマンドを実行します。
archパラメータをRaspberry Pi 3系ならarmv7、Raspberry Pi 1系ならarmv6を指定します
ansible-playbook -i inventory node-exporter.yml

node-exporter.yml
- hosts: node-exporter
  vars:
    - password: prometheus
    - arch: armv7
  tasks:
    - name: create prometheus group
      group:
        name: prometheus
      become: true
    - name: create prometheus user
      user:
        name: prometheus
        group: prometheus
        password: "{{ password | password_hash('sha512') }}"
      become: true
    - name: create prometheus directory
      file:
        path: /opt/prometheus
        state: directory
        owner: prometheus
      become: true
    - name: download prometheus node exporter
      get_url:
        url: https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-{{ arch }}.tar.gz
        dest: /opt/prometheus/
      become: true
      become_user: prometheus
    - name: extract prometheus node exporter
      unarchive:
        remote_src: yes
        src: /opt/prometheus/node_exporter-0.16.0.linux-{{ arch }}.tar.gz
        dest: /opt/prometheus/
      become: true
      become_user: prometheus
    - name: symlink
      file:
        path: /bin/node_exporter
        state: link
        src: /opt/prometheus/node_exporter-0.16.0.linux-{{ arch }}/node_exporter
      become: true
    - name: clean up
      file:
        state: absent
        path: /opt/prometheus/node_exporter-0.16.0.linux-{{ arch }}.tar.gz
      become: yes
      become_user: prometheus
    - name: setup systemd
      blockinfile:
        dest: /etc/systemd/system/node-exporter.service
        create: yes
        block: |
          [Unit]
          Description=Prometheus Node Exporter
          Requires=network.target
          [Service]
          Restart=always
          WorkingDirectory=/opt/prometheus/node_exporter-0.16.0.linux-{{ arch }}
          ExecStart=/bin/node_exporter
          ExecReload=/bin/kill -HUP $MAINPID
          [Install]
          WantedBy=multi-user.target
      become: true
    - name: enable and start node-exporter
      systemd:
        daemon_reload: yes
        enabled: yes
        state: started
        name: node-exporter.service
      become: true


○関連情報
・Ansibleに関する他の記事はこちらを参照してください。

2018年9月16日日曜日

DockerでGrafanaとPrometheusのコンテナを構築・実行する

PrometheusとGrafanaでサーバーを監視して、各種情報を可視化する事ができます。

〇Grafanaの画面


〇コンテナの構築・実行
1.prometheus.ymlを環境に合わせて編集します

prometheus.ymlの例
global:
  scrape_interval:     15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets:
        - '192.168.1.10:9100'

2. コンテナの構築と実行
以下のdocker-compose.ymlと上記のprometheus.ymlを使用して、GrafanaとPrometheusのコンテナを構築・実行します。
docker-compose up -d

docker-compose.yml
version: "2"
services:
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus-data:/prometheus
    ports:
      - "9090:9090"
  grafana:
    image: grafana/grafana
    container_name: grafana
    ports:
      - "3000:3000"
    depends_on:
      - prometheus
    environment:
      GF_SECURITY_ADMIN_USER: admin
      GF_SECURITY_ADMIN_PASSWORD: admin
volumes:
  prometheus-data:
    driver: local

3. Grafanaにアクセス
ブラウザからhttp://<DockerホストまたはIP:3000/にアクセスします。

2018年9月6日木曜日

VagrantでMySQL Server Exporter(Prometheus)とMariaDBをインストールした仮想マシン(CentOS7.5)を構築する

MySQL Server Exporterで、Prometheusの為にMariaDBの監視を行うことができます。

〇MySQL Server ExporterのデータをPrometheusで表示した画面


〇構築方法
以下のVagrantfileを使用して、MySQL Server Exporter(Prometheus)とMariaDBをインストールした仮想マシン(CentOS7.5)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.5"
  config.vm.hostname = "co75mariadbmysqlexporter"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "co75mariadbmysqlexporter"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.106", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.106", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
localectl set-locale LANG=ja_JP.UTF-8
timedatectl set-timezone Asia/Tokyo

# install mariadb
yum -y install mariadb mariadb-server
sudo systemctl enable mariadb.service
sudo systemctl start mariadb.service
mysql -uroot -e "SET PASSWORD = PASSWORD('root'); FLUSH PRIVILEGES;"
mysql -uroot -proot -e "CREATE USER prometheus@localhost IDENTIFIED BY 'prometheus';"
mysql -uroot -proot -e "GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'prometheus'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"

# install prometheus mysql exporter
groupadd prometheus
useradd -g prometheus -s /bin/bash -d /home/prometheus -m prometheus

mkdir -p /opt/prometheus
cd /opt/prometheus
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
tar xvfz mysqld_exporter-0.11.0.linux-amd64.tar.gz

ln -s /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64/mysqld_exporter /bin/mysqld_exporter

cat << EOF > /etc/systemd/system/mysqld-exporter.service
[Unit]
Description=Prometheus MySQL Server Exporter
Requires=network.target
[Service]
Restart=always
WorkingDirectory=/opt/prometheus/mysqld_exporter-0.11.0.linux-amd64
Environment=DATA_SOURCE_NAME=prometheus:prometheus@(localhost:3306)/
ExecStart=/bin/mysqld_exporter
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl enable mysqld-exporter.service
systemctl start mysqld-exporter.service
echo 'port 9104'
SHELL
end

2018年9月2日日曜日

VagrantでMySQL Server Exporter(Prometheus)とMariaDBをインストールした仮想マシン(Ubuntu18.04)を構築する

MySQL Server Exporterで、Prometheusの為にMariaDBの監視を行うことができます。

〇MySQL Server ExporterのデータをPrometheusで表示した画面


〇構築方法
以下のVagrantfileを使用して、MySQL Server Exporter(Prometheus)とMariaDBをインストールした仮想マシン(Ubuntu18.04)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804mariadbmysqlexporter"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804mariadbmysqlexporter"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.106", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.106", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
timedatectl set-timezone Asia/Tokyo

# install mariadb
echo "mariadb-server-10.0 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mariadb-server-10.0 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mariadb-server
mysql -uroot -e "CREATE USER prometheus@localhost IDENTIFIED BY 'prometheus';"
mysql -uroot -e "GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'prometheus'@'localhost';"
mysql -uroot -e "FLUSH PRIVILEGES;"

# install prometheus mysql exporter
groupadd prometheus
useradd -g prometheus -s /bin/bash -d /home/prometheus -m prometheus

mkdir -p /opt/prometheus
cd /opt/prometheus
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
tar xvfz mysqld_exporter-0.11.0.linux-amd64.tar.gz

ln -s /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64/mysqld_exporter /bin/mysqld_exporter

cat << EOF > /etc/systemd/system/mysqld-exporter.service
[Unit]
Description=Prometheus MySQL Server Exporter
Requires=network.target
[Service]
Restart=always
WorkingDirectory=/opt/prometheus/mysqld_exporter-0.11.0.linux-amd64
Environment=DATA_SOURCE_NAME=prometheus:prometheus@(localhost:3306)/
ExecStart=/bin/mysqld_exporter
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl enable mysqld-exporter.service
systemctl start mysqld-exporter.service
echo 'port 9104'
SHELL
end

2018年8月29日水曜日

VagrantでMySQL Server Exporter(Prometheus)とMariaDBをインストールした仮想マシン(Ubuntu16.04)を構築する

MySQL Server Exporterで、Prometheusの為にMariaDBの監視を行うことができます。

〇MySQL Server ExporterのデータをPrometheusで表示した画面


〇構築方法
以下のVagrantfileを使用して、MySQL Server Exporter(Prometheus)とMariaDBをインストールした仮想マシン(Ubuntu16.04)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604mariadbmysqlexporter"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604mariadbmysqlexporter"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.106", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.106", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
timedatectl set-timezone Asia/Tokyo

# install mariadb
echo "mariadb-server-10.0 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mariadb-server-10.0 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mariadb-server
mysql -uroot -e "CREATE USER prometheus@localhost IDENTIFIED BY 'prometheus';"
mysql -uroot -e "GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'prometheus'@'localhost';"
mysql -uroot -e "FLUSH PRIVILEGES;"

# install prometheus mysql exporter
groupadd prometheus
useradd -g prometheus -s /bin/bash -d /home/prometheus -m prometheus

mkdir -p /opt/prometheus
cd /opt/prometheus
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
tar xvfz mysqld_exporter-0.11.0.linux-amd64.tar.gz

ln -s /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64/mysqld_exporter /bin/mysqld_exporter

cat << EOF > /etc/systemd/system/mysqld-exporter.service
[Unit]
Description=Prometheus MySQL Server Exporter
Requires=network.target
[Service]
Restart=always
WorkingDirectory=/opt/prometheus/mysqld_exporter-0.11.0.linux-amd64
Environment=DATA_SOURCE_NAME=prometheus:prometheus@(localhost:3306)/
ExecStart=/bin/mysqld_exporter
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl enable mysqld-exporter.service
systemctl start mysqld-exporter.service
echo 'port 9104'
SHELL
end

2018年8月25日土曜日

VagrantでMySQL Server Exporter(Prometheus)とMariaDBをインストールした仮想マシン(Debian Stretch/9.5)を構築する

MySQL Server Exporterで、Prometheusの為にMariaDBの監視を行うことができます。

〇MySQL Server ExporterのデータをPrometheusで表示した画面


〇構築方法
以下のVagrantfileを使用して、MySQL Server Exporter(Prometheus)とMariaDBをインストールした仮想マシン(Debian Stretch/9.5)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.5"
  config.vm.hostname = "db95mariadbmysqldexporter"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db95mariadbmysqldexporter"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.106", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.106", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
timedatectl set-timezone Asia/Tokyo

# install mariadb
echo "mariadb-server-10.1 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mariadb-server-10.1 mysql-server/root_password_again password root" | sudo debconf-set-selections
apt-get -y install mariadb-server
mysql -uroot -e "CREATE USER prometheus@localhost IDENTIFIED BY 'prometheus';"
mysql -uroot -e "GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'prometheus'@'localhost';"
mysql -uroot -e "FLUSH PRIVILEGES;"

# install prometheus mysql exporter
groupadd prometheus
useradd -g prometheus -s /bin/bash -d /home/prometheus -m prometheus

mkdir -p /opt/prometheus
cd /opt/prometheus
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
tar xvfz mysqld_exporter-0.11.0.linux-amd64.tar.gz

ln -s /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64/mysqld_exporter /bin/mysqld_exporter

cat << EOF > /etc/systemd/system/mysqld-exporter.service
[Unit]
Description=Prometheus MySQL Server Exporter
Requires=network.target
[Service]
Restart=always
WorkingDirectory=/opt/prometheus/mysqld_exporter-0.11.0.linux-amd64
Environment=DATA_SOURCE_NAME=prometheus:prometheus@(localhost:3306)/
ExecStart=/bin/mysqld_exporter
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl enable mysqld-exporter.service
systemctl start mysqld-exporter.service
echo 'port 9104'
SHELL
end

2018年8月24日金曜日

VagrantでGrafanaとPrometheusをインストールした仮想マシン(CentOS7.5)を構築する

GrafanaとPrometheusを組み合わせて使用して、サーバーの監視、可視化を行う事ができます。

〇Grafanaの画面


〇構築方法
1. 以下のVagrantfileを使用して、GrafanaとPrometheusをインストールした仮想マシン(CentOS7.5)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.5"
  config.vm.hostname = "co75grafanaprometheus"
config.vm.network :public_network, ip:"192.168.1.107"
config.vm.network "private_network", ip: "192.168.55.107", :netmask => "255.255.255.0"
  config.vm.provider :virtualbox do |vbox|
    vbox.name = "co75grafanaprometheus"
    vbox.cpus = 4
    vbox.memory = 4096
    vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  config.vm.provision "shell", inline: <<-SHELL
localectl set-locale LANG=ja_JP.UTF-8

# download and install prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz
tar xvfz prometheus-2.3.2.linux-amd64.tar.gz
mv prometheus-2.3.2.linux-amd64 /opt

cat << EOF > /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Requires=network.target

[Service]
Restart=always
WorkingDirectory=/opt/prometheus-2.3.2.linux-amd64
ExecStart=/opt/prometheus-2.3.2.linux-amd64/prometheus --config.file=prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
systemctl enable prometheus
systemctl start prometheus

# install grafana
cat << EOF > /etc/yum.repos.d/grafana.repo
[grafana]
name=grafana
baseurl=https://packagecloud.io/grafana/stable/el/7/x86_64
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packagecloud.io/gpg.key https://grafanarel.s3.amazonaws.com/RPM-GPG-KEY-grafana
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOF
yum -y install grafana

systemctl enable grafana-server
systemctl start grafana-server

echo 'prometheus -> 192.168.1.107:9090'
echo 'grafana -> 192.168.1.107:3000' 
echo 'user: admin    password: admin'
SHELL
end

2. ブラウザからhttp://192.168.1.107:3000にアクセスします。デフォルトユーザadmin、デフォルトパスワードadminでログインします。


3.新しいパスワードを入力します


4.Add data sourceをクリックします


5. Nameにprometheus、Typeにprometheus、URLにhttp://localhost:9090を入力してSave & Testボタンをクリックします


6. Backボタンをクリックします


7. 画面左のプラスアイコンメニュー内のDashboardをクリックして、Graphアイコンをクリックします


8. Panel Title右の下矢印メニューのEditをクリックします


9. process_open_fdsを選択します


10. 画面右上の戻るボタンをクリックしてグラフの追加を終了します


2018年8月21日火曜日

AnsibleでMariaDBをインストールしたサーバにMySQL Server Exporter(Prometheus)をインストールする(CentOS7用)

MySQL Server Exporter(Prometheus)でMySQLの情報の収集を行います。

〇インストール方法
1.下準備
~/.ansible.cfgに以下の内容を設定します
[ssh_connection]
pipelining=True
[defaults]
host_key_checking = False

2. インストール対象ホストのユーザやパスワードを環境に合わせてinventoryファイルに記入します

inventory例
[mysql-exporter]
192.168.1.106

[all:vars]
ansible_ssh_port=22
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant
ansible_sudo_pass=vagrant

3.inventory mysql-exporter-deb.ymlを準備し、以下のコマンドを実行します。
ansible-playbook -i inventory mysql-exporter.yml

mysql-exporter.yml
- hosts: mysql-exporter
  vars:
    - password: prometheus
    - dbuser: prometheus
    - dbpassword: prometheus
    - dbhost: localhost
    - dbport: 3306
  tasks:
    - name: create prometheus group
      group:
        name: prometheus
      become: true
    - name: create prometheus user
      user:
        name: prometheus
        group: prometheus
        password: "{{ password | password_hash('sha512') }}"
      become: true
    - name: create prometheus directory
      file:
        path: /opt/prometheus
        state: directory
        owner: prometheus
      become: true
    - name: download mysql server exporter
      get_url:
        url: https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
        dest: /opt/prometheus/
      become: true
      become_user: prometheus
    - name: extract prometheus node exporter
      unarchive:
        remote_src: yes
        src: /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64.tar.gz
        dest: /opt/prometheus/
      become: true
      become_user: prometheus
    - name: symlink
      file:
        path: /bin/mysqld_exporter
        state: link
        src: /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64/mysqld_exporter
      become: true
    - name: clean up
      file:
        state: absent
        path: /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64.tar.gz
      become: yes
      become_user: prometheus
    - name: setup systemd
      blockinfile:
        dest: /etc/systemd/system/mysqld-exporter.service
        create: yes
        block: |
          [Unit]
          Description=Prometheus MySQL Server Exporter
          Requires=network.target
          [Service]
          Restart=always
          WorkingDirectory=/opt/prometheus/mysqld_exporter-0.11.0.linux-amd64
          Environment=DATA_SOURCE_NAME={{ dbuser }}:{{ dbpassword }}@({{ dbhost }}:{{ dbport }})/
          ExecStart=/bin/mysqld_exporter
          ExecReload=/bin/kill -HUP $MAINPID
          [Install]
          WantedBy=multi-user.target
      become: true
    - name: install epel-release
      yum:
        name: epel-release
        state: present
      become: yes
    - name: Install required software
      yum: name={{ item }} state=present enablerepo=epel
      with_items:
        - mariadb-devel
        - python-devel
        - python36-devel
        - python-pip
      become: true
    - name: install MySQL-python using pip
      pip:
        name: "{{ item }}"
        state: forcereinstall
      with_items:
        - MySQL-python
      become: true
    - name: create and grant a database user
      mysql_user:
        name={{ dbuser }}
        password={{ dbpassword }}
        priv="*.*:PROCESS,REPLICATION CLIENT,SELECT"
        state=present
      become: true
    - name: enable and start mysqld-exporter
      systemd:
        daemon_reload: yes
        enabled: yes
        state: started
        name: mysqld-exporter.service
      become: true

VagrantでPrometheusをインストールした仮想マシン(CentOS7.5)を構築する

Prometheusでサーバーの各種情報を監視する事ができます。

〇Prometheusの画面

ブラウザからhttp://192.168.1.107:9090/にアクセスします。

〇構築方法
以下のVagrantfileを使用して、Prometheusをインストールした仮想マシン(CentOS7.5)を構築します。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.5"
  config.vm.hostname = "co75prometheus"
config.vm.network :public_network, ip:"192.168.1.107"
config.vm.network "private_network", ip: "192.168.55.107", :netmask => "255.255.255.0"
  config.vm.provider :virtualbox do |vbox|
    vbox.name = "co75prometheus"
    vbox.cpus = 4
    vbox.memory = 4096
    vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
  config.vm.provision "shell", inline: <<-SHELL
localectl set-locale LANG=ja_JP.UTF-8

# download and install prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz
tar xvfz prometheus-2.3.2.linux-amd64.tar.gz
mv prometheus-2.3.2.linux-amd64 /opt

cat << EOF > /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Requires=network.target

[Service]
Restart=always
WorkingDirectory=/opt/prometheus-2.3.2.linux-amd64
ExecStart=/opt/prometheus-2.3.2.linux-amd64/prometheus --config.file=prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
systemctl enable prometheus
systemctl start prometheus

echo 'access -> http://192.168.1.107:9090/'
SHELL
end

2018年8月19日日曜日

VagrantでMySQL Server Exporter(Prometheus)とMySQLをインストールした仮想マシン(CentOS7.5)を構築する

MySQL Server Exporterで、Prometheusの為にMySQLの監視を行うことができます。

〇MySQL Server ExporterのデータをPrometheusで表示した画面


〇構築方法
以下のVagrantfileを使用して、MySQL Server Exporter(Prometheus)とMySQLをインストールした仮想マシン(CentOS7.5)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/centos-7.5"
  config.vm.hostname = "co75mysqlexporter"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "co75mysqlexporter"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.106", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.106", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
localectl set-locale LANG=ja_JP.UTF-8
timedatectl set-timezone Asia/Tokyo

# install mysql
sudo yum -y remove mariadb-libs
yum -y localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum -y install yum-utils
yum -y install mysql mysql-server
sudo systemctl enable mysqld.service
sudo systemctl start mysqld.service
# change password and create users and databases.
chkconfig mysqld on
service mysqld start
export MYSQL_ROOTPWD='Root123#'
export MYSQL_PWD=`cat /var/log/mysqld.log | awk '/temporary password/ {print $NF}'`
mysql -uroot --connect-expired-password -e "SET PASSWORD = PASSWORD('Root123#'); FLUSH PRIVILEGES;"
mysql -uroot -pRoot123# -e "UNINSTALL PLUGIN validate_password;"
mysql -uroot -pRoot123# -e "SET PASSWORD = PASSWORD('root'); FLUSH PRIVILEGES;"
mysql -uroot -proot -e "CREATE USER prometheus@localhost IDENTIFIED BY 'prometheus';"
mysql -uroot -proot -e "GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'prometheus'@'localhost';"
mysql -uroot -proot -e "FLUSH PRIVILEGES;"

# install prometheus mysql exporter
groupadd prometheus
useradd -g prometheus -s /bin/bash -d /home/prometheus -m prometheus

mkdir -p /opt/prometheus
cd /opt/prometheus
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
tar xvfz mysqld_exporter-0.11.0.linux-amd64.tar.gz

ln -s /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64/mysqld_exporter /bin/mysqld_exporter

cat << EOF > /etc/systemd/system/mysqld-exporter.service
[Unit]
Description=Prometheus MySQL Server Exporter
Requires=network.target
[Service]
Restart=always
WorkingDirectory=/opt/prometheus/mysqld_exporter-0.11.0.linux-amd64
Environment=DATA_SOURCE_NAME=prometheus:prometheus@(localhost:3306)/
ExecStart=/bin/mysqld_exporter
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl enable mysqld-exporter.service
systemctl start mysqld-exporter.service
echo 'port 9104'
SHELL
end

2018年8月18日土曜日

VagrantでGrafanaとPrometheusをインストールした仮想マシン(Ubuntu18.04)を構築する

GrafanaとPrometheusを組み合わせて使用して、サーバーの監視、可視化を行う事ができます。

〇Grafanaの画面


〇構築方法
1. 以下のVagrantfileを使用して、GrafanaとPrometheusをインストールした仮想マシン(Ubuntu18.04)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804grafanaprometheus"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804grafanaprometheus"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.109", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.109", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

# download and install prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz
tar xvfz prometheus-2.3.2.linux-amd64.tar.gz
mv prometheus-2.3.2.linux-amd64 /opt

cat << EOF > /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Requires=network.target

[Service]
Restart=always
WorkingDirectory=/opt/prometheus-2.3.2.linux-amd64
ExecStart=/opt/prometheus-2.3.2.linux-amd64/prometheus --config.file=prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
systemctl enable prometheus
systemctl start prometheus

# install grafana
apt-get -y install curl apt-transport-https
echo "deb https://packagecloud.io/grafana/stable/debian/ stretch main" >> /etc/apt/sources.list
curl https://packagecloud.io/gpg.key | sudo apt-key add -
apt-get update
apt-get -y install grafana

systemctl enable grafana-server
systemctl start grafana-server

echo 'prometheus -> 192.168.1.109:9090'
echo 'grafana -> 192.168.1.109:3000' 
echo 'user: admin    password: admin'
SHELL
end

2. ブラウザからhttp://192.168.1.109:3000にアクセスします。デフォルトユーザadmin、デフォルトパスワードadminでログインします。


3.新しいパスワードを入力します


4.Add data sourceをクリックします


5. Nameにprometheus、Typeにprometheus、URLにhttp://localhost:9090を入力してSave & Testボタンをクリックします


6. Backボタンをクリックします


7. 画面左のプラスアイコンメニュー内のDashboardをクリックして、Graphアイコンをクリックします


8. Panel Title右の下矢印メニューのEditをクリックします


9. process_virtual_memory_bytesを選択します


10. 画面右上の戻るボタンをクリックしてグラフの追加を終了します


2018年8月16日木曜日

AnsibleでMySQL Server Exporter(Prometheus)をインストールする(Debian stretch用)

MySQL Server Exporter(Prometheus)でMySQLの情報の収集を行います。

〇インストール方法
1.下準備
~/.ansible.cfgに以下の内容を設定します
[ssh_connection]
pipelining=True
[defaults]
host_key_checking = False

2. インストール対象ホストのユーザやパスワードを環境に合わせてinventoryファイルに記入します

inventory例
[mysql-exporter]
192.168.1.106

[all:vars]
ansible_ssh_port=22
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant
ansible_sudo_pass=vagrant

3.inventory mysql-exporter-deb.ymlを準備し、以下のコマンドを実行します。
ansible-playbook -i inventory mysql-exporter.yml

mysql-exporter.yml
- hosts: mysql-exporter
  vars:
    - password: prometheus
    - dbuser: prometheus
    - dbpassword: prometheus
    - dbhost: localhost
    - dbport: 3306
  tasks:
    - name: create prometheus group
      group:
        name: prometheus
      become: true
    - name: create prometheus user
      user:
        name: prometheus
        group: prometheus
        password: "{{ password | password_hash('sha512') }}"
      become: true
    - name: create prometheus directory
      file:
        path: /opt/prometheus
        state: directory
        owner: prometheus
      become: true
    - name: download mysql server exporter
      get_url:
        url: https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
        dest: /opt/prometheus/
      become: true
      become_user: prometheus
    - name: extract prometheus node exporter
      unarchive:
        remote_src: yes
        src: /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64.tar.gz
        dest: /opt/prometheus/
      become: true
      become_user: prometheus
    - name: symlink
      file:
        path: /bin/mysqld_exporter
        state: link
        src: /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64/mysqld_exporter
      become: true
    - name: clean up
      file:
        state: absent
        path: /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64.tar.gz
      become: yes
      become_user: prometheus
    - name: setup systemd
      blockinfile:
        dest: /etc/systemd/system/mysqld-exporter.service
        create: yes
        block: |
          [Unit]
          Description=Prometheus MySQL Server Exporter
          Requires=network.target
          [Service]
          Restart=always
          WorkingDirectory=/opt/prometheus/mysqld_exporter-0.11.0.linux-amd64
          Environment=DATA_SOURCE_NAME={{ dbuser }}:{{ dbpassword }}@({{ dbhost }}:{{ dbport }})/
          ExecStart=/bin/mysqld_exporter
          ExecReload=/bin/kill -HUP $MAINPID
          [Install]
          WantedBy=multi-user.target
      become: true
    - name: Install required software
      apt: name={{ item }} state=present
      with_items:
        - default-libmysqlclient-dev
        - python-pip
      become: true
    - name: install MySQL-python using pip
      pip:
        name: "{{ item }}"
        state: forcereinstall
      with_items:
        - pip
        - MySQL-python
      become: true
    - name: create and grant a database user
      mysql_user:
        name={{ dbuser }}
        password={{ dbpassword }}
        priv="*.*:PROCESS,REPLICATION CLIENT,SELECT"
        state=present
      become: true
    - name: enable and start mysqld-exporter
      systemd:
        daemon_reload: yes
        enabled: yes
        state: started
        name: mysqld-exporter.service
      become: true

2018年8月15日水曜日

Vagrant - Prometheusをインストールした仮想マシン(Ubuntu18.04)を構築する

Prometheusでサーバーの各種情報を監視する事ができます。

〇Prometheusの画面

ブラウザからhttp://192.168.1.109:9090/にアクセスします。

〇構築方法
以下のVagrantfileを使用して、Prometheusをインストールした仮想マシン(Ubuntu18.04)を構築します。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.hostname = "ub1804prometheus"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1804prometheus"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.109", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.109", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

# download and install prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz
tar xvfz prometheus-2.3.2.linux-amd64.tar.gz
mv prometheus-2.3.2.linux-amd64 /opt

cat << EOF > /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Requires=network.target

[Service]
Restart=always
WorkingDirectory=/opt/prometheus-2.3.2.linux-amd64
ExecStart=/opt/prometheus-2.3.2.linux-amd64/prometheus --config.file=prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
systemctl enable prometheus
systemctl start prometheus

echo 'access -> http://192.168.1.109:9090/'
SHELL
end

2018年8月14日火曜日

VagrantでMySQL Server Exporter(Prometheus)とMySQLをインストールした仮想マシン(Ubuntu16.04)を構築する

MySQL Server Exporterで、Prometheusの為にMySQLの監視を行うことができます。

〇MySQL Server ExporterのデータをPrometheusで表示した画面


〇構築方法
以下のVagrantfileを使用して、MySQL Server Exporter(Prometheus)とMySQLをインストールした仮想マシン(Ubuntu16.04)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604mysqlexporter"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604mysqlexporter"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.106", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.106", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
timedatectl set-timezone Asia/Tokyo

# install mysql
apt-get -y install libaio1 libmecab2
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-common_5.7.22-1ubuntu16.04_amd64.deb
dpkg -i mysql-common_5.7.22-1ubuntu16.04_amd64.deb
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client_5.7.22-1ubuntu16.04_amd64.deb
dpkg -i mysql-community-client_5.7.22-1ubuntu16.04_amd64.deb
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-client_5.7.22-1ubuntu16.04_amd64.deb
dpkg -i mysql-client_5.7.22-1ubuntu16.04_amd64.deb
export DEBIAN_FRONTEND=noninteractive
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server_5.7.22-1ubuntu16.04_amd64.deb
echo "mysql-server-5.7 mysql-server/root_password password root" | sudo debconf-set-selections
echo "mysql-server-5.7 mysql-server/root_password_again password root" | sudo debconf-set-selections
dpkg -i mysql-community-server_5.7.22-1ubuntu16.04_amd64.deb
apt-get -y install -f
mysql -uroot -e "CREATE USER prometheus@localhost IDENTIFIED BY 'prometheus';"
mysql -uroot -e "GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'prometheus'@'localhost';"
mysql -uroot -e "FLUSH PRIVILEGES;"

# install prometheus mysql exporter
groupadd prometheus
useradd -g prometheus -s /bin/bash -d /home/prometheus -m prometheus

mkdir -p /opt/prometheus
cd /opt/prometheus
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
tar xvfz mysqld_exporter-0.11.0.linux-amd64.tar.gz

ln -s /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64/mysqld_exporter /bin/mysqld_exporter

cat << EOF > /etc/systemd/system/mysqld-exporter.service
[Unit]
Description=Prometheus MySQL Server Exporter
Requires=network.target
[Service]
Restart=always
WorkingDirectory=/opt/prometheus/mysqld_exporter-0.11.0.linux-amd64
Environment=DATA_SOURCE_NAME=prometheus:prometheus@(localhost:3306)/
ExecStart=/bin/mysqld_exporter
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl enable mysqld-exporter.service
systemctl start mysqld-exporter.service
echo 'port 9104'
SHELL
end

2018年8月12日日曜日

VagrantでGrafanaとPrometheusをインストールした仮想マシン(Ubuntu16.04)を構築する

GrafanaとPrometheusを組み合わせて使用して、サーバーの監視、可視化を行う事ができます。

〇Grafanaの画面


〇構築方法
1. 以下のVagrantfileを使用して、GrafanaとPrometheusをインストールした仮想マシン(Ubuntu16.04)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604grafanaprometheus"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604grafanaprometheus"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.108", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.108", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

# download and install prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz
tar xvfz prometheus-2.3.2.linux-amd64.tar.gz
mv prometheus-2.3.2.linux-amd64 /opt

cat << EOF > /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Requires=network.target

[Service]
Restart=always
WorkingDirectory=/opt/prometheus-2.3.2.linux-amd64
ExecStart=/opt/prometheus-2.3.2.linux-amd64/prometheus --config.file=prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
systemctl enable prometheus
systemctl start prometheus

# install grafana
apt-get -y install curl apt-transport-https
echo "deb https://packagecloud.io/grafana/stable/debian/ stretch main" >> /etc/apt/sources.list
curl https://packagecloud.io/gpg.key | sudo apt-key add -
apt-get update
apt-get -y install grafana

systemctl enable grafana-server
systemctl start grafana-server

echo 'prometheus -> 192.168.1.108:9090'
echo 'grafana -> 192.168.1.108:3000'
echo 'user: admin    password: admin'
SHELL
end

2. ブラウザからhttp://192.168.1.108:3000にアクセスします。デフォルトユーザadmin、デフォルトパスワードadminでログインします。


3.新しいパスワードを入力します


4.Add data sourceをクリックします


5. Nameにprometheus、Typeにprometheus、URLにhttp://localhost:9090を入力してSave & Testボタンをクリックします


6. Backボタンをクリックします


7. 画面左のプラスアイコンメニュー内のDashboardをクリックして、Graphアイコンをクリックします


8. Panel Title右の下矢印メニューのEditをクリックします


9. go_memstats_alloc_bytesを選択します


10. 画面右上の戻るボタンをクリックしてグラフの追加を終了します


2018年8月10日金曜日

VagrantでMySQL Server Exporter(Prometheus)とMySQLをインストールした仮想マシン(Debian Stretch/9.5)を構築する

MySQL Server Exporterで、Prometheusの為にMySQLの監視を行うことができます。

〇MySQL Server ExporterのデータをPrometheusで表示した画面


〇構築方法
以下のVagrantfileを使用して、MySQL Server Exporter(Prometheus)とMySQLをインストールした仮想マシン(Debian Stretch/9.5)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.5"
  config.vm.hostname = "db95mysqldexporter"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db95mysqldexporter"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.106", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.106", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
timedatectl set-timezone Asia/Tokyo

# install mysql
wget https://dev.mysql.com/get/mysql-apt-config_0.8.9-1_all.deb
export DEBIAN_FRONTEND=noninteractive
echo mysql-apt-config mysql-apt-config/enable-repo select mysql-5.7-dmr | sudo debconf-set-selections
dpkg -i mysql-apt-config_0.8.9-1_all.deb
apt-get update
apt-get -y install mysql-server
mysql -uroot -e "CREATE USER prometheus@localhost IDENTIFIED BY 'prometheus';"
mysql -uroot -e "GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'prometheus'@'localhost';"
mysql -uroot -e "FLUSH PRIVILEGES;"

# install prometheus mysql exporter
groupadd prometheus
useradd -g prometheus -s /bin/bash -d /home/prometheus -m prometheus

mkdir -p /opt/prometheus
cd /opt/prometheus
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
tar xvfz mysqld_exporter-0.11.0.linux-amd64.tar.gz

ln -s /opt/prometheus/mysqld_exporter-0.11.0.linux-amd64/mysqld_exporter /bin/mysqld_exporter

cat << EOF > /etc/systemd/system/mysqld-exporter.service
[Unit]
Description=Prometheus MySQL Server Exporter
Requires=network.target
[Service]
Restart=always
WorkingDirectory=/opt/prometheus/mysqld_exporter-0.11.0.linux-amd64
Environment=DATA_SOURCE_NAME=prometheus:prometheus@(localhost:3306)/
ExecStart=/bin/mysqld_exporter
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl enable mysqld-exporter.service
systemctl start mysqld-exporter.service
echo 'port 9104'
SHELL
end

2018年8月7日火曜日

VagrantでPrometheusをインストールした仮想マシン(Ubuntu16.04)を構築する

Prometheusでサーバーの各種情報を監視する事ができます。

〇Prometheusの画面

ブラウザからhttp://192.168.1.108:9090/にアクセスします。

〇構築方法
以下のVagrantfileを使用して、Prometheusをインストールした仮想マシン(Ubuntu16.04)を構築します。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "ub1604prometheus"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "ub1604prometheus"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.108", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.108", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get update
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade

# download and install prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz
tar xvfz prometheus-2.3.2.linux-amd64.tar.gz
mv prometheus-2.3.2.linux-amd64 /opt

cat << EOF > /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Requires=network.target

[Service]
Restart=always
WorkingDirectory=/opt/prometheus-2.3.2.linux-amd64
ExecStart=/opt/prometheus-2.3.2.linux-amd64/prometheus --config.file=prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
systemctl enable prometheus
systemctl start prometheus

echo 'access -> http://192.168.1.108:9090/'
SHELL
end

2018年8月4日土曜日

VagrantでGrafanaとPrometheusをインストールした仮想マシン(Debian Stretch/9.4)を構築する

GrafanaとPrometheusを組み合わせて使用して、サーバーの監視、可視化を行う事ができます。

〇Grafanaの画面


〇構築方法
1. 以下のVagrantfileを使用して、GrafanaとPrometheusをインストールした仮想マシン(Debian Stretch/9.4)を構築します

Vagrantfile
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.4"
  config.vm.hostname = "db94grafanaprometheus"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db94grafanaprometheus"
     vbox.cpus = 4
     vbox.memory = 4096
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.108", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.108", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
# update packages
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
locale-gen ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8

# download and install prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.3.2/prometheus-2.3.2.linux-amd64.tar.gz
tar xvfz prometheus-2.3.2.linux-amd64.tar.gz
mv prometheus-2.3.2.linux-amd64 /opt

cat << EOF > /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus 
Requires=network.target

[Service]
Restart=always
WorkingDirectory=/opt/prometheus-2.3.2.linux-amd64
ExecStart=/opt/prometheus-2.3.2.linux-amd64/prometheus --config.file=prometheus.yml
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
systemctl enable prometheus
systemctl start prometheus

# install grafana
apt-get -y install curl apt-transport-https
echo "deb https://packagecloud.io/grafana/stable/debian/ stretch main" >> /etc/apt/sources.list
curl https://packagecloud.io/gpg.key | sudo apt-key add -
apt-get update
apt-get -y install grafana

systemctl enable grafana-server
systemctl start grafana-server

echo 'prometheus -> 192.168.1.108:9090'
echo 'grafana -> 192.168.1.108:3000'
echo 'user: admin    password: admin'
SHELL
end

2. ブラウザからhttp://192.168.1.108:3000にアクセスします。デフォルトユーザadmin、デフォルトパスワードadminでログインします。


3.新しいパスワードを入力します


4.Add data sourceをクリックします


5. Nameにprometheus、Typeにprometheus、URLにhttp://localhost:9090を入力してSave & Testボタンをクリックします


6. Backボタンをクリックします


7. 画面左のプラスアイコンメニュー内のDashboardをクリックして、Graphアイコンをクリックします


8. Panel Title右の下矢印メニューのEditをクリックします


9. go_memstats_frees_totalを選択します


10. 画面右上の戻るボタンをクリックしてグラフの追加を終了します