19.4.4 スクリプト起動契機の取得
ここでは,eventmonitorモジュールのget_exec_trigger()関数を使用して,動作中のスクリプトから,自身が起動した要因(イベント起動スクリプトの場合は発生イベント)を取得する方法を説明します。
- 〈この項の構成〉
(1) スクリプトファイルの例
イベント起動スクリプトの起動要因(発生イベント)を取得するスクリプトファイルの例を次に示します。
import sys import extlib.eventmonitor …1 dict = extlib.eventmonitor.get_exec_trigger () …2 if dict['type'] == extlib.eventmonitor.APPLET : …3 # アプレット if dict['applet']['type'] == extlib.eventmonitor.TIMER_EVT : …4 # タイマイベント if dict['applet']['condition'][extlib.eventmonitor.TIMER_TYPE] == \ extlib.eventmonitor.CRON : # cronタイマ # cron監視条件の文字列を表示 print("[condition]",file=sys.stderr) print(dict['applet']['condition'][extlib.eventmonitor.CRON],file=sys.stderr) elif dict['applet']['condition'][extlib.eventmonitor.TIMER_TYPE] == \ extlib.eventmonitor.INTERVAL : # intervalタイマ # interval監視条件の文字列を表示 print("[condition]",file=sys.stderr) print(dict['applet']['condition'][extlib.eventmonitor.INTERVAL], file=sys.stderr) elif dict['applet']['type'] == extlib.eventmonitor.SYSMSG_EVT : …5 # 運用メッセージイベント ## 運用メッセージ監視条件の表示 print("[condition]",file=sys.stderr) ## イベントレベル print("SYSMSG_EVENT_LEVEL:" + str(dict['applet']['condition'] [extlib.eventmonitor.SYSMSG_EVENT_LEVEL]),file=sys.stderr) ## イベント発生要因の運用メッセージを表示 print("[trigger system message]",file=sys.stderr) ## 発生時刻 print("SYSMSG_TIME:" + dict['applet']['trigger'] [extlib.eventmonitor.SYSMSG_TIME],file=sys.stderr) ## メッセージ識別子 print("SYSMSG_MSG_ID:" + str(hex(dict['applet']['trigger'] [extlib.eventmonitor.SYSMSG_MSG_ID])),file=sys.stderr) sys.exit() |
-
モジュールをインポートします。
-
起動要因(発生イベント)を取得する関数を呼び出します。
-
スクリプトの起動要因がアプレット機能(イベント起動スクリプト)かどうか判定します。
-
起動要因がタイマ監視の場合の監視条件を取得します。
-
起動要因が運用メッセージ監視の場合の監視条件,および起動要因となった運用メッセージの情報を取得します。