Creating server storage check with Jenkins pipeline and Mattermost notification
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
pipeline { agent any environment { stageName = "Jenkins Health Check" mattermostChannel = "#mattermost-channel" diskUsagePercent = 0 diskUsagePercentMax = 75 } stages { stage('Check Disk Space Usage') { steps { script { // /dev/mapper/vg0-fs0vol 1.0T 576G 449G 57% def cmd = """ check_string=`df -h /dev/mapper/vg0-fs0vol` ; check_result=`"\${check_string}" |& tail -1` ; IFS=' ' read -ra check_array <<< "\$check_result" ; percent=` echo "\${check_array[4]:0:2}" ` ; percent=`echo \$percent | sed "s/%/ /g"` ; percent=`echo -e \$percent | sed -e 's/[[:space:]]*\$//'` ; echo "Disk usage: \$percent%"; echo "token""==\$percent==" """ sshPublisher( continueOnError: false, failOnError: true, publishers: [ sshPublisherDesc( configName: 'jenkins.server.com', transfers: [ sshTransfer( execCommand: ''' cd /local/home/jenkins/ ''' ), sshTransfer( remoteDirectory: '', removePrefix: '', sourceFiles: '*/**', execCommand: cmd ) ], verbose: true ) ] ) } } } stage('CheckLog') { steps { script { def list = manager.build.logFile.readLines() for (i = 1; i <100; i++) { def JobCount = list.count {it.startsWith("token") && it.contains("token==${i}==")} echo("JobCount ${JobCount} at ${i}") if (JobCount == 1){ diskUsagePercent = i def maxInt = Integer.parseInt(diskUsagePercentMax) if(diskUsagePercent >= maxInt){ error("token==${diskUsagePercent}==") } } } } } } } // Finishing Up Actions post { success { script { //notifySucc(stageName, diskUsagePercent) echo(stageName) } } failure { script { notifyError(stageName, diskUsagePercent) } } unstable { script { notifyUnstable(stageName, diskUsagePercent) } } fixed { script { notifyFixed(stageName, diskUsagePercent) } } } } def notifySucc(String stageName = '', Integer diskUsagePercent = 0) { def message = ":beach_umbrella: $stageName is OK. Disk usage: $diskUsagePercent% " echo(message) mattermostSend channel: mattermostChannel, color: "good", message: message } def notifyError(String stageName = '', Integer diskUsagePercent = 0) { def message message = ":hammer_and_wrench: $stageName is warning. Disk usage: $diskUsagePercent%. :rocket: @hui.li GO GO GO to clean :rocket:." echo(message) mattermostSend channel: mattermostChannel, color: "danger", message: message emailext ( subject: message, to: "user@test.com;", body: """<p>$stageName Need Cleanup: '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p> <p>Disk space usage warning, please check "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""" ) } def notifyUnstable(String stageName = '', Integer diskUsagePercent = 0) { def message = ":warning: $stageName is aborted." echo(message) mattermostSend channel: mattermostChannel, color: "warning", message: message } def notifyFixed(String stageName = '', Integer diskUsagePercent = 0) { def message = ":sun_with_face: $stageName is back, Jenkins has been cleaned up. Disk usage: $diskUsagePercent%" echo(message) mattermostSend channel: mattermostChannel, color: "good", message: message } |