2
1

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 3 years have passed since last update.

zabbix4.4のメディアSlackでchat.getPermalinkが失敗する件

Last updated at Posted at 2020-04-24

zabbix4.4のメディアSlackでgetPermalinkが失敗する件

zabbixからslackに通知をしたいと、4.4のデフォルトのメディアを使うと、chat.getPermalinkのgetリクエストで、レスポンスの解析(JSON.parse(req.Get(url)))に失敗するので、非常に悩んだ末に解決した話

エラー

テストすると、「Slack notification failed : SyntaxError: invalid json (at offset 1)」と。。。

エラー箇所

function getPermalink(channelId, messageTimestamp) {

	var req = new CurlHttpRequest();
	req.AddHeader('Content-Type: application/x-www-form-urlencoded; charset=utf-8');

    // ここで落ちる
    var resp = JSON.parse(req.Get(
        '{0}?token={1}&channel={2}&message_ts={3}'.format(
            Slack.getPermalink,
            params.bot_token,
            channelId,
            messageTimestamp
        );
    ));
	if (req.Status != 200 && !resp.ok) {
		throw resp.error;
	}

	return resp.permalink;
}

正解

辿り着いたのはこのissue
https://support.zabbix.com/browse/ZBX-17518

「response = request.Get(url);」だとエラーを返すよ!

「response = request.Get(url, '');」だと成功するよ!

CurlHttpRequest#getには、第二引数を渡すべし!

function getPermalink(channelId, messageTimestamp) {

	var req = new CurlHttpRequest();
	req.AddHeader('Content-Type: application/x-www-form-urlencoded; charset=utf-8');

    var resp = JSON.parse(req.Get(
        '{0}?token={1}&channel={2}&message_ts={3}'.format(
            Slack.getPermalink,
            params.bot_token,
            channelId,
            messageTimestamp
        ), "" // 第2引数を!
    ));
	if (req.Status != 200 && !resp.ok) {
		throw resp.error;
	}

	return resp.permalink;
}

参考

テンプレートはこれ
https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/media/slack/media_slack.xml

テスト実行時の最小限のパラメータ

  • zabbix_url
    http://zabbix.hogehoge.com/zabbix

  • bot_token
    Bot User OAuth Token

  • channel
    #zabbix

  • slack_mode
    alarm

  • slack_as_user
    true

  • slack_endpoint
    https://slack.com/api/

  • event_tags
    {EVENT.TAGS}

  • event_name
    {EVENT.NAME}

  • event_nseverity
    {EVENT.NSEVERITY}

  • event_ack_status
    {EVENT.ACK.STATUS}

  • event_value
    1

  • event_update_status
    1

  • event_date
    {EVENT.DATE}

  • event_time
    {EVENT.TIME}

  • event_severity
    {EVENT.SEVERITY}

  • event_opdata
    {EVENT.OPDATA}

  • event_id
    1

  • event_update_message
    {EVENT.UPDATE.MESSAGE}

  • trigger_id
    {TRIGGER.ID}

  • trigger_description
    {TRIGGER.DESCRIPTION}

  • host_name
    {HOST.NAME}

  • host_ip
    {HOST.IP}

  • event_update_date
    {EVENT.UPDATE.DATE}

  • event_update_time
    {EVENT.UPDATE.TIME}

  • event_recovery_date
    {EVENT.RECOVERY.DATE}

  • event_recovery_time
    {EVENT.RECOVERY.TIME}

  • event_source
    3

  • host_conn
    {HOST.CONN}

非常に参考になったサイト(感謝!)

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?