Zabbixで東電のでんき予報データを監視する方法

(追記)
すーばらしい、Zabbixの寺島さんがスクリプト公開してくれてらっしゃる!
Kodai's Blog: Zabbixで電力会社の電力使用状況を監視する
参照:

YahooのAPIを使うほうがサーバが安定してそうだけれども、5分に1回監視する程度なら、東電でもええではないか、ということでメモ。

データ取得

wgetで取得するスクリプトをcronで動かす。(コマンドのパスはCentOS5のもの。curlのほうがいいかしらん)

#!/bin/bash
TMP=`mktemp`
AWK=/bin/awk
WGET=/usr/bin/wget
LATEST=/path_to/電気監視最新.txt
LIMIT=/path_to/電気監視上限.txt

$WGET -O $TMP http://www.tepco.co.jp/forecast/html/images/juyo-j.csv > /dev/null
$AWK -F\, '{if ($3 > 0) {last=$3}}END{print last}' $TMP  > $LATEST
head -3 $TMP | tail -1 | $AWK -F\, "{print \$1}" > $LIMIT
rm -f $TMP

↑のコードで最新の値($LATEST)を拾ってくるところは、5分間隔で更新されているデータのほうから取得している。このあたりが汚いと感じる人はYahooのAPIをお勧めする。ただしJSONとかをparseしないといけない。
たとえば、Android(Java)ではこんなかんじ。Javascriptでやれば全然楽なはずw

package foobar;
// importうんぬんは省略
public class ActivitiyName extends Activity {

	private ProgressDialog progressDialog;
	private TextView output;
	private Button button;
	private CookieStore cookieStore = new BasicCookieStore();
	private final Handler handler = new Handler() {
		public void handleMessage(final Message msg) {
			progressDialog.dismiss();
			String bundleResult = msg.getData().getString("RESPONSE");
			try {
				JSONObject json = new JSONObject(bundleResult);
				JSONObject usage = json.getJSONObject("ElectricPowerUsage");
				JSONObject usageString = usage.getJSONObject("Usage");
				JSONObject capacityString = usage.getJSONObject("Capacity");
				String datestring = usage.getString("Date");
				String hourstring = usage.getString("Hour");
				int usageValue = usageString.getInt("$");
				int capacityValue = capacityString.getInt("$");
				output.setText("as of " + datestring + " " + hourstring + "oclock\n" + 
						       "Usage: " + String.valueOf(usageValue) + "\n" +
						       String.valueOf(capacityValue) + "kW");
				} catch (JSONException e) {
// エラー処理
				}
		}
	};

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.output = (TextView) findViewById(R.id.Response);
		this.button = (Button) findViewById(R.id.Button);
		
		this.button.setOnClickListener(new OnClickListener() {
			public void onClick(final View v) {
				output.setText(".....");
				performRequest("YahooのAPIのURL");
			}
		});
	}

	private void performRequest(final String url) {
		final ResponseHandler<String> responseHandler = new ResponseHandler<String> () {
			public String handleResponse(HttpResponse response) {
				HttpEntity entity = response.getEntity();
				String result =null;
				try {
					result = StringUtils.inputStreamToString(entity.getContent());
					Message message = handler.obtainMessage();
					Bundle bundle = new Bundle();
					bundle.putString("RESPONSE", result);
					message.setData(bundle);
					handler.sendMessage(message);
				} catch (Exception e) {
// エラー処理
				}
				return result;
			}
		};

		this.progressDialog = ProgressDialog.show(this, "working . . .",
				"performing HTTP request");

		// do the HTTP dance in a separate thread (the responseHandler will fire
		// when complete)
		new Thread() {
			@Override
			public void run() {
				HttpClient httpclient = new DefaultHttpClient();
				try {

					// Create local HTTP context
					HttpContext localContext = new BasicHttpContext();
					// Bind custom cookie store to the local context
					localContext.setAttribute(ClientContext.COOKIE_STORE,
							cookieStore);

					final HttpPost httppost = new HttpPost(url);

					// Pass local context as a parameter
					String response = httpclient.execute(httppost, responseHandler,localContext);
					
				} catch (Exception e) {
// エラー処理
				}
			}
		}.start();
	}
}

でZabbix側では、、、

サーバ上にファイルとして書き出してしまえば、zabbix_agentd.conf に以下のような記述をするだけでキーとしてアクセスできるようになる。

UserParameter=latest.tepco, cat /path_to/電気監視最新.txt
UserParameter=max.tepco, cat /path_to/電気監視上限.txt

あとはこのキーを使ってアイテムを作り、トリガーを設定する。
たとえば、5分前の使用電力が、供給量の95%を超えたらアラート、とかいうのは以下のようになる。

{サーバ名:latest.tepco.last(#1)}>{サーバ名:max.tepco.last(#1)}*0.95

Zabbixで監視するならそもそもの東電サーバとの導通をみとかなきゃな

そして、依存関係にしとく。(あとでやる)