{"id":2855,"date":"2022-09-05T07:40:15","date_gmt":"2022-09-05T07:40:15","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=2855"},"modified":"2023-08-04T16:11:12","modified_gmt":"2023-08-04T16:11:12","slug":"python-slack","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/python-slack","title":{"rendered":"Python script to get slack notification for &#8220;Out of Memory&#8221; string in log file"},"content":{"rendered":"<p>In this blog, we will create a\u00a0 <a href=\"https:\/\/www.checkmateq.com\/python-development\">python script<\/a> to get a slack notification when &#8220;Out of memory&#8221; string is found in the log file.<\/p>\n<h3><strong>Step1: Slack Configuration<\/strong><\/h3>\n<ul>\n<li>First, you need to have a slack account and a workspace.<\/li>\n<li>Please visit the following link:\u00a0<a href=\"https:\/\/api.slack.com\/\">https:\/\/api.slack.com\/<\/a><\/li>\n<li>Sign in to your slack account.<\/li>\n<li>Click on\u00a0<strong><em>Create an app<\/em><\/strong><\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2873\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies-300x82.png\" alt=\"\" width=\"746\" height=\"204\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies-300x82.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies-1024x280.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies-768x210.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies-1536x421.png 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies-1200x329.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-web-development-agencies.png 1797w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<ul>\n<li>Next, click create from scratch.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2874\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-database-management-company-300x217.png\" alt=\"\" width=\"744\" height=\"538\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-database-management-company-300x217.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-database-management-company.png 759w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<ul>\n<li>Write an app name and select your workspace.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2875\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-blockchain-development-company-295x300.png\" alt=\"\" width=\"750\" height=\"763\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-blockchain-development-company-295x300.png 295w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-blockchain-development-company.png 729w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<ul>\n<li>Select<strong> Bots<\/strong> in <strong>Add features and functionality<\/strong>.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2877\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-DevOps-outsourcing-company-300x166.png\" alt=\"\" width=\"743\" height=\"411\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-DevOps-outsourcing-company-300x166.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-DevOps-outsourcing-company-768x426.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-DevOps-outsourcing-company.png 954w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<ul>\n<li>Click <strong>Review scopes to add<\/strong>.<\/li>\n<li>Click Add an OAuth scope in Bot token scopes.<\/li>\n<li>Select <strong>chat:write<\/strong> scope.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2879\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-AI-software-development-services-300x151.png\" alt=\"\" width=\"747\" height=\"376\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-AI-software-development-services-300x151.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-AI-software-development-services-768x387.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-AI-software-development-services.png 982w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<ul>\n<li>Next, click<strong> Install to workspace\u00a0<\/strong>and select <strong>Allow<\/strong>.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2880\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-saas-development-company-300x173.png\" alt=\"\" width=\"735\" height=\"424\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-saas-development-company-300x173.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-saas-development-company-768x444.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-saas-development-company.png 888w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<ul>\n<li>Copy the<strong> Bot User OAuth Token<\/strong>.<\/li>\n<li>Now go to your slack workspace and create a new channel.<\/li>\n<li>Click<strong> Integrations-&gt;Add an app<\/strong>.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2882\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-middleware-development-company-300x260.png\" alt=\"\" width=\"740\" height=\"641\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-middleware-development-company-300x260.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-middleware-development-company.png 841w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<ul>\n<li>Add the app that we created.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2883\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-WordPress-development-services-300x127.png\" alt=\"\" width=\"737\" height=\"312\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-WordPress-development-services-300x127.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-WordPress-development-services-1024x435.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-WordPress-development-services-768x326.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-WordPress-development-services.png 1033w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<h3>Step2: Write the python script<\/h3>\n<p>This script will send the slack notification if the &#8220;Out of memory&#8221; string is detected in the log file. We will use the <strong>awk<\/strong> command to get logs of only the last five minutes so that we do not receive notifications for older logs.<\/p>\n<pre><code>\r\nimport slack\r\nimport subprocess\r\n\r\ncmd = \"sudo awk -v d1=\\\"$(date --date=\\\"-5 min\\\" \\\"+%b %_d %H:%M\\\")\\\" -v d2=\\\"$(date \\\"+%b %_d %H:%M\\\")\\\" '$0 &gt; d1 &amp;&amp; $0 &lt; d2 || $0 ~ d2' \/var\/log\/messages |grep -i 'out of memory'\"\r\n\r\nsp=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)\r\nrc=sp.wait()\r\nif sp.poll() :\r\n     pass\r\nelse:\r\n     stdout,stderr=sp.communicate()\r\n     message=stdout\r\n     client= slack.WebClient(token='Auth Token')\r\n     client.chat_postMessage(channel='#channel-name', text=f\"{message}\")\r\n<\/code><\/pre>\n<ul>\n<li>Replace channel name and Auth token in the script.<\/li>\n<\/ul>\n<h3>Step3: Create a cron job<\/h3>\n<ul>\n<li>Next, we will create a cronjob so that our script runs every five minutes.<\/li>\n<li>Use the following command to open crontab.<\/li>\n<\/ul>\n<pre>sudo crontab -e<\/pre>\n<ul>\n<li>\u00a0Paste the following command in the crontab.<\/li>\n<\/ul>\n<pre>*\/5 * * * *  \/usr\/bin\/python3 \/home\/vagrant\/scripts\/out-of-memory.py\r\n<\/pre>\n<ul>\n<li>To test the script we will use the<strong> stress<\/strong> command to increase memory load.<\/li>\n<\/ul>\n<pre>stress --vm 50 --timeout 200s\r\n<\/pre>\n<ul>\n<li>Now check your slack channel, you should receive a notification in some time.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2891\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-offshore-software-development-300x53.png\" alt=\"\" width=\"758\" height=\"134\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-offshore-software-development-300x53.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-offshore-software-development-1024x179.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-offshore-software-development-768x135.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-offshore-software-development-1200x210.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-offshore-software-development.png 1507w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>Author Information\u00a0<\/strong><\/p>\n<p>This is blog is written by Checkmate Global Technologies engineer. <a href=\"https:\/\/www.checkmateq.com\/\">Please contact<\/a> our technical consultants to discuss about product engineering, product development, IT staff augmentation and cloud infrastructure.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will create a\u00a0 python script to get a slack notification when &#8220;Out of memory&#8221; string is found in the log file. Step1: Slack Configuration First, you need to have a slack account and a workspace. Please visit the following link:\u00a0https:\/\/api.slack.com\/ Sign in to your slack account. Click on\u00a0Create an app Next, &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/python-slack\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python script to get slack notification for &#8220;Out of Memory&#8221; string in log file&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":2916,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[2,71,70,69,68,7,23],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2855"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=2855"}],"version-history":[{"count":18,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2855\/revisions"}],"predecessor-version":[{"id":4245,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2855\/revisions\/4245"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/2916"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=2855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=2855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=2855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}