6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ZabbixAdvent Calendar 2015

Day 18

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

Last updated at Posted at 2015-12-17

この記事は 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さんです。
よろしくお願いします。

6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?