1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SNMPエージェントをCheckMKで監視する

1
Posted at

前回ESP32をSNMPエージェントにしました。(ESP32をSNMPエージェントにする
今回は、これをSNMPマネージャであるCheckMKで監視します。

・CheckMKのインストール
・CheckMKのセットアップ
・ESP32の独自OIDの監視設定

CheckMKのインストール

https://github.com/Checkmk/checkmk
https://docs.checkmk.com/latest/en/introduction_docker.html

あらかじめ、以下のフォルダを作っておきます。SNMPの独自OIDを監視するためのプラグインを置くためです。

$ mkdir -p /opt/checkmk/sites

Dockerでインストールします。

docker-compose.yaml
services:
  checkmk:
    image: "checkmk/check-mk-raw:2.4.0-latest"
    container_name: "checkmk"
    environment:
      - TZ=Asia/Tokyo
      - CMK_PASSWORD=mypassword
    volumes:
      - /opt/checkmk/sites:/omd/sites
    tmpfs:
      - /opt/omd/sites/cmk/tmp:uid=1000,gid=1000
    ports:
      - 5051:5000
      - 5081:8000
    restart: always

CheckMKのセットアップ

ブラウザから以下のURLを開きます。

http://[インストールしたサーバのホスト名]:5051/

image.png

Username: cmkadmin
Password: mypassword

[Setup]→[Users]
AdministratorのRoleのユーザを追加
cmkadminユーザを無効化(disable the login to this account=off)

Administratorのユーザで再ログイン

[Setup]→[Hosts]
「Add folder」でlocalnetを作成

Title:localnet
Checkmk agent / API integrations: No API integrations, no Checkmk agent
SNMP: SNMP v1
Network scan:
IP range to scan: IP network, 192.168.1.0/24
Scan interval: 10 days 0 hours

最後にSaveで保存します。

しばらくたつと、localnetフォルダに、検出されたホストがリストアップされています。
監視したいホストのPropertiesを表示し、Custom attributesのCriticalityを無効にします。

ESP32の独自OIDの監視設定

Dockerにログインする。

$ docker exec -it checkmk bash
> su – cmk
> cmk -L

たくさん表示されたらOKです。

以下のフォルダに2つのファイルを置きます。

/omd/sites/cmk/local/lib/python3/cmk_addons/plugins/esp32quickjs/agent_based/

esp32quickjs_number.py
#!/usr/bin/env python3
# This file is explained in the Checkmk User Guide:
# https://docs.checkmk.com/master/en/devel_check_plugins_snmp.html#simple_snmp_plugin
#
# Store in your Checkmk site at:
# ~/local/lib/python3/cmk_addons/plugins/esp32quickjs/agent_based/esp32quickjs_number.py

from cmk.agent_based.v2 import (
    CheckPlugin,
    CheckResult,
    startswith,
    DiscoveryResult,
    Result,
    Service,
    SimpleSNMPSection,
    SNMPTree,
    State,
    StringTable,
    Metric,
)

def parse_esp32quickjs(string_table):
    if not string_table or len(string_table[0]) < 3:
        return None

    return {
        "number_0": int(string_table[0][0]),
        "number_1": int(string_table[0][1]),
        "number_2": int(string_table[0][2]),
    }

def discover_esp32quickjs(section):
    if section is not None:
        yield Service()

def check_esp32quickjs(section):
    if section is None:
        yield Result(state=State.UNKNOWN, summary="No data received")
        return
    
    n0 = section.get("number_0")
    n1 = section.get("number_1")
    n2 = section.get("number_2")

    yield Metric(name="number_0", value=n0)
    yield Metric(name="number_1", value=n1)
    yield Metric(name="number_2", value=n2)
    
    yield Result(state=State.OK, summary=f"Values: {n0}, {n1}, {n2}. All information is available.")

snmp_section_esp32quickjs_setup = SimpleSNMPSection(
    name = "esp32quickjs_number_config",
    parse_function = parse_esp32quickjs,
    detect = startswith(
        ".1.3.6.1.2.1.1.1.0",
        "ESP32 SNMP Agent",
    ),
    fetch = SNMPTree(
        base = '.1.3.6.1.4.1.8072.9999.1',
        oids = ['0', '1', '2'],
    ),
)

check_plugin_esp32quickjs_setup = CheckPlugin(
    name = "esp32quickjs_number",
    sections = [ "esp32quickjs_number_config" ],
    service_name = "ESP32_Quickjs Number",
    discovery_function = discover_esp32quickjs,
    check_function = check_esp32quickjs,
)
esp32quickjs_string.py
#!/usr/bin/env python3
# This file is explained in the Checkmk User Guide:
# https://docs.checkmk.com/master/en/devel_check_plugins_snmp.html#simple_snmp_plugin
#
# Store in your Checkmk site at:
# ~/local/lib/python3/cmk_addons/plugins/esp32quickjs/agent_based/esp32quickjs_string.py

from cmk.agent_based.v2 import (
    CheckPlugin,
    CheckResult,
    startswith,
    DiscoveryResult,
    Result,
    Service,
    SimpleSNMPSection,
    SNMPTree,
    State,
    StringTable,
)

def parse_esp32quickjs(string_table):
    if not string_table or len(string_table[0]) < 3:
        return None
    
    return {
        "string_0": string_table[0][0],
        "string_1": string_table[0][1],
        "string_2": string_table[0][2],
    }

def discover_esp32quickjs(section):
    if section is not None:
        yield Service()

def check_esp32quickjs(section):
    if section is None:
        yield Result(state=State.UNKNOWN, summary="No data received")
        return

    s0 = section.get("string_0")
    s1 = section.get("string_1")
    s2 = section.get("string_2")
        
    yield Result(state=State.OK, summary=f"Values: {s0}, {s1}, {s2}. All information is available.")

snmp_section_esp32quickjs_setup = SimpleSNMPSection(
    name = "esp32quickjs_string_config",
    parse_function = parse_esp32quickjs,
    detect = startswith(
        ".1.3.6.1.2.1.1.1.0",
        "ESP32 SNMP Agent",
    ),
    fetch = SNMPTree(
        base = '.1.3.6.1.4.1.8072.9999.2',
        oids = ['0', '1', '2'],
    ),
)

check_plugin_esp32quickjs_setup = CheckPlugin(
    name = "esp32quickjs_string",
    sections = [ "esp32quickjs_string_config" ],
    service_name = "ESP32_Quickjs String",
    discovery_function = discover_esp32quickjs,
    check_function = check_esp32quickjs,
)

cmk --debug -vII --no-cache 192.168.1.XXX を実行し、実行エラーがなければ成功です。

esp32quickjs_numberとesp32quickjs_stringが検出されているのがわかります。

$cmk --debug -vII --no-cache 192.168.1.XXX
Discovering services and host labels on: 192.168.1.XXX
192.168.1.XXX:
+ FETCHING DATA
Get piggybacked data
+ ANALYSE DISCOVERED HOST LABELS
SUCCESS - Found no host labels
+ ANALYSE DISCOVERED SERVICES
+ EXECUTING DISCOVERY PLUGINS (8)
  1 esp32quickjs_number
  1 esp32quickjs_string
  1 mem_used
  1 snmp_info
  1 uptime
SUCCESS - Found 5 services

Monitorページで見ると、こんな感じで値の変化が可視化されています。

image.png

ESP32側では、以下のようなJavascriptで、値を変化させたときのものです。

main.js
import * as lcd from "Lcd";
import * as env from "Env";
import * as wire from "Wire";

console.log(JSON.stringify(esp32.getMemoryUsage()));
console.log(JSON.stringify(esp32.getStorageInfo()));

function setup(){
	lcd.clear();

	wire.begin(32, 33);

	console.log("setup finished");
	lcd.setCursor(0, 0);
	lcd.print("setup finished");

	env.sht40_begin();
}

setInterval(() =>{
	var value = env.sht40_get();
	console.log(JSON.stringify(value));
	esp32.setSnmpNumber(0, Math.round(value.temperature * 10));
	esp32.setSnmpNumber(1, Math.round(value.humidity * 10));
}, 1000);

function loop(){
	esp32.update();
}

(参考) ESP32で動作するJavascript実行環境

ESP32で動作するJavascript実行環境を公開しています。
この中で実装しています。

「電子書籍:M5StackとJavascriptではじめるIoTデバイス制御」

サポートサイト

以上

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?