💻

AWSのEC2インスタンスの監視のためUbuntu22.04にcloudwatchagentをインストールする
September 14, 2022February 20, 2024
#Develop#Ubuntu#AWS

はじめに

AWS の EC2 インスタンスを cloudwatch で監視した際に Ubuntu22.0 で苦労したのでメモ
EC2 は理由がないかぎり AmazonLinux を使おう

環境

  • Ubuntu22.04
  • Amazon EC2

EC2 からダウンロード&インストール

wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
sudo dpkg -i ./amazon-cloudwatch-agent.deb

IAM ロール設定

CloudWatch からデータを読めるようにするために IAM の設定をする。 まずは適用するためのロールを作成する。

IAM ロールの作成

IAM -> ロール ロールを作成
信頼されたエンティティタイプ:AWSのサービス
一般的なユースケース:EC2 ->次へ
ポリシー名:CloudWatchAgentServerPolicy ->次へ
ロール名(任意):CloudWatchAgentServerRole ->ロールを作成

### インスタンスにロールを適用

EC2 -> インスタンス -> 対象インスタンス -> アクション -> セキュリティ -> IAMロールの変更
IAMロール:CloudWatchAgentServerRole -> IAMロールの更新

Cloud Watch Agent を起動する

こけた

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:configuration-file-path
****** processing amazon-cloudwatch-agent ******
/opt/aws/amazon-cloudwatch-agent/bin/config-downloader --output-dir /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d --download-source file:configuration-file-path --mode ec2 --config /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml --multi-config default
I! Trying to detect region from ec2
D! [EC2] Found active network interface
2022/09/14 11:06:50 E! Fail to fetch/remove json config: open configuration-file-path: no such file or directory
E! Fail to fetch/remove json config: open configuration-file-path: no such file or directory
Fail to fetch the config!

collectd が必要らしい

[小ネタ]Amazon Linux 2 で CloudWatch エージェントを起動しようとしてハマった話 | DevelopersIO

エージェント 起動でハマって ああ悲し

クラスメソッド発「やってみた」系技術メディア | DevelopersIO

[小ネタ]Amazon Linux 2 で CloudWatch エージェントを起動しようとしてハマった話 | DevelopersIO
sudo apt install collectd
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package collectd is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'collectd' has no installation candidate

失敗した Ubuntu22 ではパッケージが削除されてる

https://www.ubuntuupdates.org/package/core/jammy/universe/proposed/collectd

collectd をビルドする

$ wget https://storage.googleapis.com/collectd-tarballs/collectd-5.12.0.tar.bz2
--2022-09-15 08:04:33--  https://storage.googleapis.com/collectd-tarballs/collectd-5.12.0.tar.bz2
Resolving storage.googleapis.com (storage.googleapis.com)... 142.250.196.112, 142.250.196.144, 142.250.199.112, ...
Connecting to storage.googleapis.com (storage.googleapis.com)|142.250.196.112|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1902756 (1.8M) [application/x-bzip]
Saving to: ‘collectd-5.12.0.tar.bz2’

collectd-5.12.0.tar.bz2                                    100%[=======================================================================================================================================>]   1.81M  --.-KB/s    in 0.05s

2022-09-15 08:04:33 (36.6 MB/s) - ‘collectd-5.12.0.tar.bz2’ saved [1902756/1902756]

$ tar xf collectd-5.12.0.tar.bz2
tar (child): lbzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

$ sudo apt install lbzip2
$ tar xf collectd-5.12.0.tar.bz2
$ cd collectd-5.12.0/
$ ./configure
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking how to print strings... printf
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/home/ubuntu/collectd-5.12.0':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

$ sudo apt install gcc libpthread-stubs0-dev build-essential libgcrypt-dev
$ ./configure
...
...

Configuration:
  Build:
    Platform  . . . . . . Linux
    Compiler vendor . . . gnu
    CC  . . . . . . . . . gcc
    CFLAGS  . . . . . . . -Wall -Werror -g -O2
    CXXFLAGS  . . . . . . -Wall -Werror -g -O2
    CPP . . . . . . . . . gcc -E
    CPPFLAGS  . . . . . .
    GRPC_CPP_PLUGIN . . .
    LD  . . . . . . . . . /usr/bin/ld -m elf_x86_64
    LDFLAGS . . . . . . .
    PROTOC  . . . . . . . /usr/bin/protoc
    YACC  . . . . . . . . yacc
    YFLAGS  . . . . . . .

  Libraries:
    intel mic . . . . . . no (MicAccessApi not found)
    libaquaero5 . . . . . no (libaquaero5.h not found)
    libatasmart . . . . . no (atasmart.h not found)
    libcurl . . . . . . . no (curl-config failed)
    libdbi  . . . . . . . no (dbi/dbi.h not found)
    libdpdk . . . . . . . no (rte_config.h not found)
    libesmtp  . . . . . . no (libesmtp.h not found)
    libganglia  . . . . . no (gm_protocol.h not found)
    libgcrypt . . . . . . yes (gcrypt.h not found)
    libgps  . . . . . . . no (gps.h not found)
    libgrpc++ . . . . . . no (pkg-config could not find libgrpc++)
    libhiredis  . . . . . no (hiredis.h not found)
    libi2c-dev  . . . . . no (symbol i2c_smbus_read_i2c_block_data not found - have you installed libi2c-dev ?)
    libiokit  . . . . . . no
    libiptc . . . . . . . no (pkg-config doesn't know libiptc)
    libjansson  . . . . . no (jansson.h not found)
    libjevents  . . . . . no (jevents.h not found)
    libjvm  . . . . . . . no (jar not found)
    libkstat  . . . . . . no (Solaris only)
    libkvm  . . . . . . . no
    libldap . . . . . . . no ('ldap.h' not found)
    liblua  . . . . . . . no (pkg-config cannot find liblua)
    libmemcached  . . . . no (libmemcached/memcached.h not found)
    libmicrohttpd . . . . no (pkg-config could not find libmicrohttpd)
    libmnl  . . . . . . . no (symbol 'mnl_nlmsg_get_payload' not found)
    libmodbus . . . . . . no (pkg-config doesn't know libmodbus)
    libmongoc . . . . . . no (pkg-config could not find libmongoc)
    libmosquitto  . . . . no (mosquitto.h not found)
    libmysql  . . . . . . no (mysql_config failed)
    libnetapp . . . . . . no (netapp_api.h not found)
    libnetsnmp  . . . . . no (net-snmp/net-snmp-includes.h not found)
    libnetsnmpagent . . . no (net-snmp/agent/net-snmp-agent-includes.h not found)
    libnotify . . . . . . no (pkg-config doesn't know libnotify)
    libnvidia-ml  . . . . no
    libopenipmi . . . . . no (pkg-config doesn't know OpenIPMIpthread)
    liboping  . . . . . . no (oping.h not found)
    libowcapi . . . . . . no (owcapi.h not found)
    libpcap . . . . . . . no (pcap.h misses PCAP_ERROR_IFACE_NOT_UP)
    libperfstat . . . . . no (AIX only)
    libperl . . . . . . . no
    libpmwapi . . . . . . no (pmw_api.h not found)
    libpq . . . . . . . . no (libpq-fe.h not found)
    libpqos . . . . . . . no (pqos.h not found)
    libprotobuf . . . . . yes
    libprotobuf-c . . . . no (libprotobuf-c not found)
    libpython . . . . . . no
    libqpid-proton .  . . no (proton/proactor.h not found)
    librabbitmq . . . . . no (amqp.h not found)
    libriemann-client . . no (pkg-config doesn't know libriemann-client)
    librdkafka  . . . . . no (librdkafka/rdkafka.h not found)
    librouteros . . . . . no (routeros_api.h not found)
    librrd  . . . . . . . no (rrd.h not found)
    libsensors  . . . . . no (sensors/sensors.h not found)
    libsigrok   . . . . . no (pkg-config could not find libsigrok)
    libssl  . . . . . . . no (ssl header not found)
    libslurm .  . . . . . no (pkg-config doesn't know libslurm)
    libstatgrab . . . . . no (pkg-config doesn't know libstatgrab)
    libtokyotyrant  . . . no (tcrdb.h not found)
    libudev . . . . . . . no (libudev.h not found)
    libupsclient  . . . . no (pkg-config doesn't know libupsclient)
    libvarnish  . . . . . no (pkg-config doesn't know varnishapi)
    libvirt . . . . . . . no (pkg-config doesn't know libvirt)
    libxenctrl  . . . . . no (xenctrl.h not found)
    libxml2 . . . . . . . no (pkg-config doesn't know libxml-2.0)
    libxmms . . . . . . . no
    libyajl . . . . . . . no (yajl/yajl_parse.h not found)
    oracle  . . . . . . . no (ORACLE_HOME is not set)
    protobuf-c  . . . . . no (protoc-c compiler not found)
    protoc 3  . . . . . . yes

  Features:
    daemon mode . . . . . yes
    debug . . . . . . . . no

  Bindings:
    perl  . . . . . . . . yes (PREFIX=NONE)

  Modules:
    aggregation . . . . . yes
    amqp    . . . . . . . no (amqp.h not found)
    amqp1   . . . . . . . no (proton/proactor.h not found)
    apache  . . . . . . . no (curl-config failed)
    apcups  . . . . . . . yes
    apple_sensors . . . . no
    aquaero . . . . . . . no (libaquaero5.h not found)
    ascent  . . . . . . . no
    barometer . . . . . . no
    battery . . . . . . . yes
    bind  . . . . . . . . no
    buddyinfo . . . . . . yes
    capabilities  . . . . no
    ceph  . . . . . . . . no
    cgroups . . . . . . . yes
    chrony. . . . . . . . yes
    check_uptime. . . . . yes
    connectivity. . . . . no
    conntrack . . . . . . yes
    contextswitch . . . . yes
    cpu . . . . . . . . . yes
    cpufreq . . . . . . . yes
    cpusleep  . . . . . . yes
    csv . . . . . . . . . yes
    curl  . . . . . . . . no (curl-config failed)
    curl_json . . . . . . no
    curl_xml  . . . . . . no
    dbi . . . . . . . . . no (dbi/dbi.h not found)
    dcpmm  . . . . . .  . no (pmw_api.h not found)
    df  . . . . . . . . . yes
    disk  . . . . . . . . yes
    dns . . . . . . . . . no (pcap.h misses PCAP_ERROR_IFACE_NOT_UP)
    dpdkevents. . . . . . no
    dpdkstat  . . . . . . no
    dpdk_telemetry. . . . no
    drbd  . . . . . . . . yes
    email . . . . . . . . yes
    entropy . . . . . . . yes
    ethstat . . . . . . . yes
    exec  . . . . . . . . yes
    fhcount . . . . . . . yes
    filecount . . . . . . yes
    fscache . . . . . . . yes
    gmond . . . . . . . . no (gm_protocol.h not found)
    gps . . . . . . . . . no
    gpu_nvidia  . . . . . no
    grpc  . . . . . . . . no (libgrpc++ not found)
    hddtemp . . . . . . . yes
    hugepages . . . . . . yes
    infiniband  . . . . . yes
    intel_pmu . . . . . . no (jevents.h not found)
    intel_rdt . . . . . . no (pqos.h not found)
    interface . . . . . . yes
    ipc . . . . . . . . . yes
    ipmi  . . . . . . . . no
    iptables  . . . . . . no (pkg-config doesn't know libiptc)
    ipstats . . . . . . . no
    ipvs  . . . . . . . . yes
    irq . . . . . . . . . yes
    java  . . . . . . . . no (jar not found)
    load  . . . . . . . . yes
    logfile . . . . . . . yes
    logparser . . . . . . yes
    log_logstash  . . . . no
    lpar  . . . . . . . . no (AIX only)
    lua . . . . . . . . . no (pkg-config cannot find liblua)
    madwifi . . . . . . . yes
    match_empty_counter . yes
    match_hashed  . . . . yes
    match_regex . . . . . yes
    match_timediff  . . . yes
    match_value . . . . . yes
    mbmon . . . . . . . . yes
    mcelog  . . . . . . . yes
    md  . . . . . . . . . yes
    mdevents  . . . . . . yes
    memcachec . . . . . . no (libmemcached/memcached.h not found)
    memcached . . . . . . yes
    memory  . . . . . . . yes
    mic . . . . . . . . . no (MicAccessApi not found)
    modbus  . . . . . . . no (pkg-config doesn't know libmodbus)
    mqtt  . . . . . . . . no (mosquitto.h not found)
    multimeter  . . . . . yes
    mysql . . . . . . . . no (mysql_config failed)
    netapp  . . . . . . . no (netapp_api.h not found)
    netlink . . . . . . . no (symbol 'mnl_nlmsg_get_payload' not found)
    netstat_udp . . . . . no
    network . . . . . . . yes
    nfs . . . . . . . . . yes
    nginx . . . . . . . . no (curl-config failed)
    notify_desktop  . . . no (pkg-config doesn't know libnotify)
    notify_email  . . . . no (libesmtp.h not found)
    notify_nagios . . . . yes
    ntpd  . . . . . . . . yes
    numa  . . . . . . . . yes
    nut . . . . . . . . . no (pkg-config doesn't know libupsclient)
    olsrd . . . . . . . . yes
    onewire . . . . . . . no (owcapi.h not found)
    openldap  . . . . . . no ('ldap.h' not found)
    openvpn . . . . . . . yes
    oracle  . . . . . . . no (ORACLE_HOME is not set)
    ovs_events  . . . . . no
    ovs_stats . . . . . . no
    pcie_errors . . . . . yes
    perl  . . . . . . . . no (needs libperl)
    pf  . . . . . . . . . no
    pinba . . . . . . . . no
    ping  . . . . . . . . no (oping.h not found)
    postgresql  . . . . . no (libpq-fe.h not found)
    powerdns  . . . . . . yes
    processes . . . . . . yes
    procevent . . . . . . no
    protocols . . . . . . yes
    python  . . . . . . . no
    redfish . . . . . . . no (redfish.h not found)
    redis . . . . . . . . no (hiredis.h not found)
    routeros  . . . . . . no (routeros_api.h not found)
    rrdcached . . . . . . no
    rrdtool . . . . . . . no (rrd.h not found)
    sensors . . . . . . . no (sensors/sensors.h not found)
    serial  . . . . . . . yes
    sigrok  . . . . . . . no (pkg-config could not find libsigrok)
    slurm . . . . . . . . no (pkg-config doesn't know libslurm)
    smart . . . . . . . . no
    snmp  . . . . . . . . no (net-snmp/net-snmp-includes.h not found)
    snmp_agent  . . . . . no (net-snmp/agent/net-snmp-agent-includes.h not found)
    statsd  . . . . . . . yes
    swap  . . . . . . . . yes
    synproxy  . . . . . . yes
    sysevent. . . . . . . no
    syslog  . . . . . . . yes
    table . . . . . . . . yes
    tail_csv  . . . . . . yes
    tail  . . . . . . . . yes
    tape  . . . . . . . . no
    target_notification . yes
    target_replace  . . . yes
    target_scale  . . . . yes
    target_set  . . . . . yes
    target_v5upgrade  . . yes
    tcpconns  . . . . . . yes
    teamspeak2  . . . . . yes
    ted . . . . . . . . . yes
    thermal . . . . . . . yes
    threshold . . . . . . yes
    tokyotyrant . . . . . no (tcrdb.h not found)
    turbostat . . . . . . yes
    ubi . . . . . . . . . yes
    unixsock  . . . . . . yes
    uptime  . . . . . . . yes
    users . . . . . . . . yes
    uuid  . . . . . . . . yes
    varnish . . . . . . . no (pkg-config doesn't know varnishapi)
    virt  . . . . . . . . no
    vmem  . . . . . . . . yes
    vserver . . . . . . . yes
    wireless  . . . . . . yes
    write_graphite  . . . yes
    write_http  . . . . . no (curl-config failed)
    write_influxdb_udp. . yes
    write_kafka . . . . . no (librdkafka/rdkafka.h not found)
    write_log . . . . . . yes
    write_mongodb . . . . no (pkg-config could not find libmongoc)
    write_prometheus. . . no
    write_redis . . . . . no (hiredis.h not found)
    write_riemann . . . . no (pkg-config doesn't know libriemann-client)
    write_sensu . . . . . yes
    write_stackdriver . . no
    write_syslog . .  . . yes
    write_tsdb  . . . . . yes
    xencpu  . . . . . . . no
    xmms  . . . . . . . . no
    zfs_arc . . . . . . . yes
    zone  . . . . . . . . no
    zookeeper . . . . . . yes

$ make
$ sudo make install

設定調整

systemctl 用の起動ファイルを作る collected.service

$cd /opt/collectd/etc
$sudo vim collectd.service
[Unit]
Description=collectd - System statistics collection daemon
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=notify
NotifyAccess=main

ExecStart=/opt/collectd/sbin/collectd
ExecStop=pkill collectd

[Install]
WantedBy=multi-user.target
$chmod 644 collectd.service
$sudo systemctl link /opt/collectd/etc/collectd.service

collectd の設定ファイルを編集する。

$sudo vim /opt/collectd/etc/collectd.conf

network プラグイン周りの設定を変更する

<Plugin network>
#       # client setup:
        Server "127.0.0.1" "25826"
        <Server "127.0.0.1" "25826">
                SecurityLevel Encrypt
                Username "user"
                Password "hogehoge"
        </Server>
</Plugin>

cloud-agent 用に auth_file を作る

$ sudo cd /opt/collectd/etc/
$ sudo vim auth_file

auth_file

user:hogehoge

cloudwatch-agent と設定ファイルの場所にシンボリックリンクを貼る

mkdir -p /etc/collectd
mkdir -p /usr/share/colle

config を作る場合下記のコマンドで作成できる

$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

今回は下記を利用する。 ウィザードを回したあと conf ファイルを置き換えた。

$ sudo vim /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/config.json
{
  "agent": {
    "metrics_collection_interval": 60,
    "run_as_user": "root"
  },
  "metrics": {
    "metrics_collected": {
      "collectd": {
        "name_prefix": "collectd_metics",
        "metrics_aggregation_interval": 120
      },
      "cpu": {
        "resources": ["*"],
        "measurement": [
          {
            "name": "cpu_usage_idle",
            "rename": "CPU_USAGE_IDLE",
            "unit": "Percent"
          },
          { "name": "cpu_usage_nice", "unit": "Percent" },
          "cpu_usage_guest"
        ],
        "totalcpu": false,
        "metrics_collection_interval": 10,
        "append_dimensions": {
          "test": "test1",
          "date": "2017-10-01"
        }
      },
      "netstat": {
        "measurement": ["tcp_established", "tcp_syn_sent", "tcp_close"],
        "metrics_collection_interval": 60
      },
      "disk": {
        "measurement": ["used_percent"],
        "resources": ["*"],
        "drop_device": true
      },
      "processes": {
        "measurement": ["running", "sleeping", "dead"]
      }
    },
    "append_dimensions": {
      "ImageId": "${aws:ImageId}",
      "InstanceId": "${aws:InstanceId}",
      "InstanceType": "${aws:InstanceType}",
      "AutoScalingGroupName": "${aws:AutoScalingGroupName}"
    },
    "aggregation_dimensions": [
      ["AutoScalingGroupName"],
      ["InstanceId", "InstanceType"],
      []
    ]
  }
}
 sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2
****** processing amazon-cloudwatch-agent ******
/opt/aws/amazon-cloudwatch-agent/bin/config-downloader --output-dir /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d --download-source file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml --mode ec2 --config /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml --multi-config default
I! Trying to detect region from ec2
D! [EC2] Found active network interface
Successfully fetched the config and saved in /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_amazon-cloudwatch-agent.toml.tmp
Start configuration validation...
/opt/aws/amazon-cloudwatch-agent/bin/config-translator --input /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json --input-dir /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d --output /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml --mode ec2 --config /opt/aws/amazon-cloudwatch-agent/etc/common-config.toml --multi-config default
2022/09/15 09:50:25 Reading json config file path: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_amazon-cloudwatch-agent.toml.tmp ...
2022/09/15 09:50:25 unable to scan config dir /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d with error: unable to parse json, error: invalid character 'a' looking for beginning of value
No json config files found, use the default one
2022/09/15 09:50:25 I! Valid Json input schema.
I! Detecting run_as_user...
I! Trying to detect region from ec2
D! [EC2] Found active network interface
No csm configuration found.
No log configuration found.
Configuration validation first phase succeeded
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent -schematest -config /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml
Configuration validation second phase succeeded
Configuration validation succeeded

参考

おしまい



sakakinox

Written by sakakinox
Server enginier

Copyright © sakakinox.net 2021-2024.