Help us understand the problem. What is going on with this article?

Zabbix イベント履歴情報の一括エクスポート

More than 3 years have passed since last update.

この記事は Zabbix Advent Calendar 2015 の18日目の記事です。
今回は小技的なものを紹介したいと思います。


Zabbixで発生したイベント履歴情報は、GUIからCSV形式でエクスポートできますが、件数が多いと複数ページに分かれてしまい何回もクリックするはめになり面倒なものです。またバッチ的に定期的に自動出力したいと思いスクリプトを作成しました。

※MySQL用です

#!/bin/bash
#################
USER='zabbix'
PASS='your_password'
DBNAME='zabbix'
STIME=`date +%s -d '30 days ago'`
ETIME=`date +%s`
#################

BASEPATH=$(cd $(dirname $0); pwd)
MY_COMM="mysql --user ${USER} --password=${PASS} ${DBNAME}"
TARGET_FILE="${BASEPATH}/zabbix_events_`date +%Y%m%d_%H-%M-%S`.csv"
TABLE_FILE="/tmp/zabbix_events.$$"

echo "\"時間\",\"ホスト\",\"説明\",\"ステータス\",\"深刻度\"" > ${TARGET_FILE}

${MY_COMM}<< EOF 
SELECT
  from_unixtime(e.clock, "%Y/%m/%d %H:%i:%s"),
  h.host,
  t.description,
  CASE
    WHEN e.value = '0' THEN '正常'
    WHEN e.value = '1' THEN '障害'
  END "status",
  CASE 
    WHEN t.priority LIKE '0' THEN '未分類'
    WHEN t.priority LIKE '1' THEN '情報'
    WHEN t.priority LIKE '2' THEN '警告'
    WHEN t.priority LIKE '3' THEN '軽度の障害'
    WHEN t.priority LIKE '4' THEN '重度の障害'
    WHEN t.priority LIKE '5' THEN '致命的な障害'
  END "priority"
FROM events e
  inner join triggers t
  inner join functions f
  inner join items i
  inner join hosts h
  ON e.objectid=t.triggerid AND t.triggerid=f.triggerid AND f.itemid=i.itemid AND i.hostid=h.hostid
WHERE e.source='0' AND e.clock BETWEEN "${STIME}" AND "${ETIME}"
ORDER BY e.clock ASC
into outfile "${TABLE_FILE}" 
character set utf8 
fields terminated by ','
OPTIONALLY ENCLOSED BY '"'; 
exit
EOF

cat "${TABLE_FILE}" >> ${TARGET_FILE}
rm -f ${TABLE_FILE}
exit

尚、トリガー名に含まれるマクロの展開はしていません。


次はqryuuさんです。
よろしくお願いします。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away