LoginSignup
2
3

More than 1 year has passed since last update.

OCI Monitoringにプロセス監視のカスタム・メトリックを公開する

Last updated at Posted at 2021-08-06

Oracle Cloud Infrastructure Monitoringにユーザーの好みのメトリックデータをアップロードするにはカスタム・メトリックの機能を使います。カスタムメトリックは1秒単位でPostすることができ、最小集計間隔は1分です。
カスタムメトリックのアップロードは、OCI CLIやAPIで実行することができますが、ここではPython SDKでアップロードする方法を紹介します。

今回のPythonスクリプトは、ドキュメントにあるサンプルコードをベースに使っていますが、1点注意があります。サンプルコード内のmonitoring_clientの箇所には、service_endpointの記述が不足しているので、正しくは以下のようにendpointを指定します。

monitoring_client = oci.monitoring.MonitoringClient(config,service_endpoint="https://telemetry-ingestion.us-ashburn-1.oraclecloud.com")

このサンプルコードをベースにして、OSで稼働する特定のプロセスの有無をチェックし、カスタムメトリックを公開するPythonスクリプトを作成。ここでは単純にvmstatのプロセスの存在有無を監視しています。

import oci,datetime,psutil,datetime
from pytz import timezone

#OCIの接続情報
config = {
    "user": "ocid1.user.oc1..xxxxxxxxxxxxxxxxx",
    "key_file": "/home/opc/.oci/oci_api_key.pem",
    "fingerprint": "XX:XX:XX:XX:XX:XX:XX:XX:c7:a2:4f:71:c0:a1:26:6e",
    "tenancy": "ocid1.tenancy.oc1..aaaaaaaawtaaxxxxxxxxxxxxxxxxxxxxx",
    "region": "us-ashburn-1"
}

# Initialize service client with default config file
monitoring_client = oci.monitoring.MonitoringClient(config,service_endpoint="https://telemetry-ingestion.us-ashburn-1.oraclecloud.com")

existvalue = 0

#検出するプロセス名を指定。サンプルとしてvmstatを検出
for proc in psutil.process_iter():
    try:
        if "vmstat" in proc.exe():
            existvalue = 1
    except psutil.AccessDenied:
        pass

dt_now = datetime.datetime.now(timezone('UTC'))

#compartment_idを指定、namespace, metric name, dimentionはお好みの値に
#プロセスが見つかれば1、なければ0をPost
post_metric_data_response = monitoring_client.post_metric_data(
    post_metric_data_details=oci.monitoring.models.PostMetricDataDetails(
        metric_data=[
            oci.monitoring.models.MetricDataDetails(
                namespace="osprocess",
                compartment_id="ocid1.compartment.oc1..aaaaaaaa6itxxxxxxxxxxx",
                name="process001",
                dimensions={
                    'serverid': 'testsrv001'},
                datapoints=[
                    oci.monitoring.models.Datapoint(
                        timestamp=datetime.datetime.strftime(
                            dt_now,"%Y-%m-%dT%H:%M:%S.%fZ"),
                        value=existvalue)]
                )]
    )
)

作成したpythonスプリプトを定期的に実行する

watch -n 30 python custometric.py

モニタリング -> メトリック・エクスプローラーを開き以下の問い合わせを実行
image.png

凹んでいる箇所がプロセスを検出できなかった期間。値が0の場合というトリガールールを設定してNotificationsでアラート通知ができる。
image.png

その他のOracle Cloud Infrastructureに関連するトピック

2
3
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
2
3