推荐使用Prometheus – 在Grafana中需要监控的用户列表
进行调查
我觉得可以搜索一下,貌似可以使用node_exporter的textfile.directory收集器创建自定义的指标。
如果要采集单独的度量数据,可以使用文本文件收集器。
似乎是说,只要按照以下形式创建一个文本文件(扩展名prom),它会自动读取。
node_scrape_collector_duration_seconds{collector="cpu"} 0.000278377
node_scrape_collector_duration_seconds{collector="diskstats"} 0.000326093
整理需要的信息。
只需要一个选项:作为信息,我想要用户名和最后登录信息。
如果整理lastlog中的信息,似乎就可以解决这个问题。
基于这个理由,最终将尝试以以下格式进行整理。
node_user_info{username="",latest="",fromip="",tty=""} 1
撰写脚本
根据上面的参考文章表示,它使用了Ruby之类的技术,但以我的技能水平来看,我只能用命令行脚本来实现…
操作系统版本:Ubuntu 16.04
#!/bin/bash
OUTPUT=/usr/local/node_exporter/textfile/userlist.prom
# Ubuntu 16.04だと作られたユーザはUID 1000から採番されるのでとりあえず1000-2999までのIDを持つものでgrep
USERLIST=`cat /etc/passwd | grep -e '[1-2][0-9][0-9][0-9]' | awk -F: '{print $1}'`
# OUTPUT 初期化
: > $OUTPUT
# 処理
for i in $USERLIST;do
NAME=$i
PTTY=`lastlog -u $i | grep $i | awk '{print $2}'`
FROMIP=`lastlog -u $i | grep $i | awk '{print $3}'`
LATEST=`lastlog -u $i | grep $i | awk '{print $9" "$5" "$6" "$7" "}'`
cat << EOF >> $OUTPUT
node_user_info{username="${NAME}",latest="${LATEST}",fromip="${FROMIP}",tty="${PTTY}"} 1
EOF
done
只要将此脚本安排在cron中运行,应该就可以了吧。
塞封的东西
不要忘记激活收藏家。
# BEGIN ANSIBLE MANAGED BLOCK
[Unit]
Description=node_exporter for Prometheus
[Service]
Restart=always
User=prometheus
ExecStart=/usr/bin/node_exporter \
--web.listen-address=0.0.0.0:9100 \
--collector.diskstats.ignored-devices="^(dm-|ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$" \
--collector.filesystem.ignored-mount-points="^/(dev|proc|sys|run|var/lib/(docker|lxcfs|nobody_tmp_secure))($|/)" \
--collector.filesystem.ignored-fs-types="^(autofs|binfmt_misc|cgroup|configfs|debugfs|devpts|devtmpfs|fuse.*|hugetlbfs|mqueue|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|sysfs|tracefs)$" \
--collector.netdev.ignored-devices="^(lo|docker[0-9]|veth.+)$" \
--no-collector.conntrack \
--collector.cpu \
--collector.diskstats \
--no-collector.filefd \
--collector.filesystem \
--collector.loadavg \
--collector.meminfo \
--collector.netdev \
--collector.netstat \
--no-collector.ntp \
--no-collector.sockstat \
--collector.stat \
- --no-colletctor.textfile \
+ --collector.textfile \
--no-collector.time \
--collector.uname \
--collector.vmstat \
--no-collector.arp \
--no-collector.bcache \
--no-collector.bonding \
--no-collector.buddyinfo \
--no-collector.drbd \
--no-collector.edac \
--no-collector.entropy \
--no-collector.hwmon \
--no-collector.infiniband \
--no-collector.interrupts \
--no-collector.ipvs \
--no-collector.ksmd \
--no-collector.logind \
--no-collector.mdadm \
--no-collector.meminfo_numa \
--no-collector.mountstats \
--no-collector.nfs \
--no-collector.nfsd \
--no-collector.qdisc \
--no-collector.runit \
--no-collector.supervisord \
--no-collector.systemd \
--no-collector.tcpstat \
--no-collector.timex \
--no-collector.wifi \
--no-collector.xfs \
--no-collector.zfs \
+ --collector.textfile.directory=/usr/local/node_exporter/textfile/
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=20s
SendSIGKILL=no
[Install]
WantedBy=multi-user.target
# END ANSIBLE MANAGED BLOCK
*/15 * * * * /usr/local/node_exporter/userlist.sh 以 cron 实现
结果
暂时先通过curl命令从自己的服务器获取指标来确认一下。
root# curl http://localhost:9100/metrics | grep user
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29320 100 29320 0 0 2003k 0 --:--:-- --:--:-- --:--:-- 2045k
node_cpu_guest_seconds_total{cpu="0",mode="user"} 0
node_cpu_seconds_total{cpu="0",mode="user"} 319.13
# HELP node_filesystem_avail_bytes Filesystem space available to non-root users in bytes.
node_textfile_mtime_seconds{file="userlist.prom"} 1.530865161e+09
# HELP node_user_info Metric read from /usr/local/node_exporter/textfile/userlist.prom
# TYPE node_user_info untyped
node_user_info{fromip="XX.XXX.XXX.210",latest="2018 Jul 6 15:11:02 ",tty="pts/2",username="XXX"} 1
node_user_info{fromip="XX.XXX.XXX.210",latest="2018 Jul 6 15:57:09 ",tty="pts/1",username="ubuntu"} 1
node_user_info{fromip="XX.XXX.XXX.210",latest="2018 Jul 6 16:37:33 ",tty="pts/3",username="XXXXX"} 1
好厉害啊,宝贝。乖乖。
在Grafana中进行列表化
变量设置
Name: 任意
Lable: instance
Hide: Variable
Query: node_user_info
Regex: .*instance="(.*?)".*
接下来,你可以根据自己的喜好选择隐藏或显示列,随你便。
请提供参考的网址链接。
如果要采集自定义指标,请使用 Textfile Collector。
请中文进行重述:
请给出以下内容的中文本地化版本,只需要提供一种选择:
– The cat is sitting on the mat.
普罗米修斯的历史记录
普罗米修斯推荐-初次尝试-
普罗米修斯推荐-服务发现-
普罗米修斯推荐-导入导出器 node-exporter(通过 apt-get)-
普罗米修斯推荐-导入导出器 node-exporter(通过二进制文件)-
普罗米修斯推荐-服务发现-尽头点变成了”http://:9100/metrics”,变成了自身的问题。