{"id":256424,"date":"2021-05-22T17:00:00","date_gmt":"2021-05-22T14:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-setup-slack-notifications-for-low-disk-space-on-your-server-cloudsavvy-it\/"},"modified":"2021-05-22T17:00:00","modified_gmt":"2021-05-22T14:00:00","slug":"how-to-setup-slack-notifications-for-low-disk-space-on-your-server-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-setup-slack-notifications-for-low-disk-space-on-your-server-cloudsavvy-it\/","title":{"rendered":"#How to Setup Slack Notifications for Low Disk Space On Your Server \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a41141eb4b2f\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a41141eb4b2f\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-setup-slack-notifications-for-low-disk-space-on-your-server-cloudsavvy-it\/#Get_Disk_Space_With_The_df_Command\" >Get Disk Space With The df Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-setup-slack-notifications-for-low-disk-space-on-your-server-cloudsavvy-it\/#Configure_Command_Line_Slack_Notifications\" >Configure Command Line Slack Notifications<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-setup-slack-notifications-for-low-disk-space-on-your-server-cloudsavvy-it\/#Putting_it_All_Together\" >Putting it All Together<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Setup Slack Notifications for Low Disk Space On Your Server \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-845\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/e489d8e1-3.png?width=1200&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You probably want to know when your server is running out of disk space. An easy solution is to configure a cron job to run daily and send you notifications over Slack if there are any issues.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Get_Disk_Space_With_The_df_Command\"><\/span>Get Disk Space With The df Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The command for viewing disk space on Linux is <code>df -h<\/code>\u00a0(for output in a human readable format):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-812 size-full\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/8cc412ad-e1561496304151.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"199\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This lists each mounted filesystem, how big the physical disk is, and how much is being used.<\/p>\n<p>To narrow the output down to a specific filesystem (in this case, root) you can run <code>df \/<\/code>. However, this returns the header row, so you can filter that off with <code>grep<\/code>:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-816\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/8cc412ad-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"200\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Then, select the fifth column (the percentage) with <code>awk<\/code>, and remove the percent symbol with <code>sed<\/code>. This gives you one command that returns a number representing how much of your main disk is full:<\/p>\n<pre>df \/ | grep \/ | awk '{ print $5}' | sed 's\/%\/\/g'<\/pre>\n<p>Which, in this case, would return \u201c<code>39<\/code>\u201c. If you want to change the disk this command monitors, you\u2019ll need to edit the first <code>df<\/code>\u00a0statement.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Configure_Command_Line_Slack_Notifications\"><\/span>Configure Command Line Slack Notifications<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The simplest way to get Slack notifications working is with webhooks. Webhooks allow you to send notifications as POST requests to a URL without having to manage bot users or OAuth. You can format messages as JSON and POST them using <code>curl<\/code>.<\/p>\n<p>From Slack\u2019s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/api.slack.com\/apps?new_app=1\">API portal<\/a>, create a new <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>, select your workspace to add it to, and give it a name. This is what will show up when it messages you, so pick something interesting.<\/p>\n<p>Once you have your app, find the \u201cIncoming Webhooks\u201d tab under \u201cAdd features and functionality.\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-295\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/05\/104eab18.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This will let you create a new webhook, and you\u2019ll have to choose which channel it should post to. You can create a new channel for bot notifications, or add it to your own DMs.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-296\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/05\/0a4a563f.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"400\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Once you have the URL, copy the sample request to verify it works. You should see \u201cHello World!\u201d in the channel you selected. If so, Slack is good to go.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Putting_it_All_Together\"><\/span>Putting it All Together<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To make your server actually notify you, you\u2019ll create a script that you can run with a daily <code>cron<\/code> job. Here\u2019s the shell of the script, minus the notification command:<\/p>\n<pre>#!\/bin\/bash&#13;\nCURRENT=$(df \/ | grep \/ | awk '{ print $5}' | sed 's\/%\/\/g')&#13;\nTHRESHOLD=90&#13;\n&#13;\nif [ \"$CURRENT\" -gt \"$THRESHOLD\" ] ; then&#13;\n&#13;\nfi<\/pre>\n<p>This script sets two variables, <code>CURRENT<\/code>\u00a0(to the output of the <code>df<\/code>\u00a0command from above) and <code>THRESHOLD<\/code>\u00a0(to a value you can configure). It then checks if the current value is greater than the threshold, and runs a command if it is.<\/p>\n<p>You can set the notification to be whatever you\u2019d like, but here\u2019s an example\u2014not including the webhook URL, which you\u2019ll need to paste at the end (no extra flag required):<\/p>\n<article id=\"post-811\" class=\"post-811 post type-post status-draft format-standard hentry category-uncategorized\">\n<div id=\"article-content-area\" class=\"entry-content\">\n<pre>curl -X POST -H 'Content-type: application\/json' --data \"{\"text\":\"Your server `$(hostname)` is currently at ${CURRENT}% disk capacity.\"}\"<\/pre>\n<p>This will send a nicely formatted notification to your webhook\u2019s designated channel, including the server\u2019s hostname and the current disk capacity.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-827\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/e489d8e1-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"100\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can paste this command in the <code>if<\/code> statement of the script above, making the whole thing come together like so:<\/p>\n<pre>#!\/bin\/bash&#13;\nCURRENT=$(df \/ | grep \/ | awk '{ print $5}' | sed 's\/%\/\/g')&#13;\nTHRESHOLD=90&#13;\n&#13;\nif [ \"$CURRENT\" -gt \"$THRESHOLD\" ] ; then&#13;\ncurl -X POST -H 'Content-type: application\/json' --data \"{\"text\":\"Your server `$(hostname)` is currently at ${CURRENT}% disk capacity.\"}\"&#13;\nfi<\/pre>\n<p>Save this as <code>diskspace.sh<\/code> in your home directory, and make sure it has execute permissions with <code>chmod +x diskpace.sh<\/code>. Open up your crontab with <code>crontab -e<\/code>, and add the following line to the end:<\/p>\n<pre>0 * * * * ~\/diskspace.sh<\/pre>\n<p>This will run your script every hour to check if your disk is full. But you\u2019ll only get notifications if it is full, so you can test if the script works by setting <code>THRESHOLD<\/code>\u00a0below the current disk usage and running it manually.<\/p>\n<p>For more detailed notifications, you can make the script send you the results of <code>df -h<\/code>, formatted nicely in a code box, just by updating the <code>curl<\/code>\u00a0statement:\n<\/div>\n<pre>curl -X POST -H 'Content-type: application\/json' --data \"{\"text\":\"Your server `$(hostname)` is currently at ${CURRENT}% disk capacity.n ```$(df -h)``` \"}\"<\/pre>\n<p>Which will show up in Slack as:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-835\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/e489d8e1-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"300\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Since the command the script runs can be anything you want it to be, you could also configure email notifications using a mail handler like Postfix, or any other feed that supports webhooks.<br \/>\n<\/article>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/811\/how-to-setup-slack-notifications-for-low-disk-space-on-your-server\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Setup Slack Notifications for Low Disk Space On Your Server \u2013 CloudSavvy IT&#8221; You probably want to know when your server is running out of disk space. An easy solution is to configure a cron job to run daily and send you notifications over Slack if there are any issues. Get Disk Space&#8230;<\/p>\n","protected":false},"author":1,"featured_media":256425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/e489d8e1-3.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-256424","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/256424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=256424"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/256424\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/256425"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=256424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=256424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=256424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}