0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ディスク容量のアラートをSlackで通知するジョブ

Last updated at Posted at 2022-07-30

ディスク容量のアラート通知を出すジョブを作りました。
定期実行すればいいかな・・?という。

/**
 * 容量チェックアラートジョブ
 */
pipeline { 
    agent {
        label "動かしたいノード" // どこのマシンの容量チェックしたいか次第
    }

    stages {
        stage('disk容量チェック') {
            steps {
                script {
                    def command = """
                    df --total -h | tail -1 | sed -r 's/^.* ([0-9].*)%.*\$/\\1/'
                    """
                    def usage = sh(script:command, returnStdout:true)
                    println 'usage:' + usage
                    usage = usage.replaceAll("\n", "")
                    int usageValue = usage as int
                    if (usageValue > 60) { // いい感じの数値で実際はやる
                        def notify_message = """:alert: `[hogeマシン]のディスク容量が${usage}%です。`
                        """
                        if (params.notify_channels != '') {
                            slackSend channel: notify_channels,
                            message: notify_message,
                            color: "danger"
                        }
                    }
                }
            }
        }
    }       
}

つまづいたとこ

jenkinsは、pipeを使うとうまくいかなかったり、sedを使ったり、jq使ったりするとうまくいかなかったりすることがあります。
'か、"かの問題があったりするんですが、私がつまづいたのは、

  1. 主にぐぐると大体出てくる方法ではなく、それ+sedコマンドに-rをつけないとだめだった
  2. 抜き出しで、\1/する時に、さらに\をつける必要があった

という部分。$とかは変数扱いになってしまうので、\つけないといけないというのはわかってたのですが、sedなんだかうまく動かないなー みたいなのはちょこちょこあります。

感想

ジョブ自体の失敗・成功通知は入れても良かったもしれないけど、どっちでもいいかな。
sedコマンドはまだまだ使い慣れてない感。

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?