<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Keshav  Maheshwari on Medium]]></title>
        <description><![CDATA[Stories by Keshav  Maheshwari on Medium]]></description>
        <link>https://medium.com/@hackermob?source=rss-599891c305fb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dZogji8LOq_NDw85g7aadw.jpeg</url>
            <title>Stories by Keshav  Maheshwari on Medium</title>
            <link>https://medium.com/@hackermob?source=rss-599891c305fb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 23:46:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hackermob/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Collect Jenkins Deployment Logs using Cloudwatch Agent]]></title>
            <link>https://medium.com/@hackermob/collect-jenkins-deployment-logs-using-cloudwatch-agent-3cecf10f1901?source=rss-599891c305fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/3cecf10f1901</guid>
            <category><![CDATA[jenkins]]></category>
            <category><![CDATA[cloudwatch]]></category>
            <category><![CDATA[aws-security]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[cloudwatch-agent]]></category>
            <dc:creator><![CDATA[Keshav  Maheshwari]]></dc:creator>
            <pubDate>Sun, 01 Jan 2023 19:18:04 GMT</pubDate>
            <atom:updated>2023-01-01T19:50:51.224Z</atom:updated>
            <content:encoded><![CDATA[<p>In this blog, we are going to discuss how we can collect Jenkins Deployment logs from Ec2(Linux) and send them to Cloudwatch using the Cloudwatch agent.</p><p>Setting up the Cloudwatch agent in the Jenkins EC2 machine:</p><p>Download and configure the Cloudwatch agent using the command line in the Jenkins EC2 machine you can download your own version as per your machine.</p><pre>wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb</pre><p>Install the downloaded package(Linux):</p><pre>sudo dpkg -i -E ./amazon-cloudwatch-agent.deb</pre><p>Assign the below AWS Managed Policy to the EC2 Instance or you can follow the least privilege principle and assign only the required access.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i3XzyUMLad4QNSgImOcBCA.png" /></figure><p>After assigning the Policy to the machine go to the Ec2 instance and generate the config.json file for the Cloudwatch agent or create a new one.</p><pre>/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n6uCfCXQzo7-4Ttpfm-Q4w.png" /></figure><p>or if you don’t want to generate the config.json file via amazon-cloudwatch-agent-config-wizard you can directly go to the folder and create the config.json file at location /opt/aws/amazon-cloudwatch-agent/bin/</p><p>and add the below content for exporting the logs to the Cloudwatch in the JSON file as per the path of the Jenkins logs being stored for the deployment which usually lies in /var/lib/jenkins/jobs/$YOUR_RESPECTIVE_PROJECTNAME/builds/$build_number/log</p><pre>{<br> &quot;agent&quot;: {<br>  &quot;run_as_user&quot;: &quot;root&quot;<br> },<br> &quot;logs&quot;: {<br>  &quot;logs_collected&quot;: {<br>   &quot;files&quot;: {<br>    &quot;collect_list&quot;: [{<br>     &quot;file_path&quot;: &quot;/var/lib/jenkins/jobs/**&quot;,<br>     &quot;log_group_name&quot;: &quot;Deployment_Logs&quot;,<br>     &quot;log_stream_name&quot;: &quot;All_Project_logs&quot;<br>    }]<br>   }<br>  }<br> }<br>}</pre><p>The above code will export all the logs for all the projects in the single stream as we have mentioned ** /var/lib/jenkins/jobs/** at the end of the file path.</p><p>Additionally, If you want to create a specific stream for the specific Jenkin Project you can use the below code and follow the process for all your projects.</p><pre>{<br> &quot;agent&quot;: {<br>  &quot;run_as_user&quot;: &quot;root&quot;<br> },<br> &quot;logs&quot;: {<br>  &quot;logs_collected&quot;: {<br>   &quot;files&quot;: {<br>    &quot;collect_list&quot;: [{<br>     &quot;file_path&quot;: &quot;/var/lib/jenkins/jobs/$Your_Specific_project_name1/builds/**&quot;,<br>     &quot;log_group_name&quot;: &quot;Deployment_Logs&quot;,<br>     &quot;log_stream_name&quot;: &quot;Project1&quot;<br>    },<br>{<br>     &quot;file_path&quot;: &quot;/var/lib/jenkins/jobs/$Your_Specific_project_name2/builds/**&quot;,<br>     &quot;log_group_name&quot;: &quot;Deployment_Logs&quot;,<br>     &quot;log_stream_name&quot;: &quot;Project2&quot;<br>    }<br>]<br>   }<br>  }<br> }<br>}</pre><p>If you have a lot of projects and want to generate the snippet for all of them you can use the below bash script to generate the file. Please also validate the Json after generating the file from the script.</p><pre>:&gt;jenkins_new.txt<br>:&gt;comparison_jenkins.txt<br>:&gt;result01.txt<br>:&gt;result02.txt<br>:&gt;comma.txt<br>echo &quot;,&quot; &gt; comma.txt<br>ls /var/lib/jenkins/jobs/ &gt;&gt; jenkins_new.txt<br>comm -3 Jenkins_old.txt jenkins_new.txt | sed &#39;s/^[ \t]*//;s/[ \t]*$//&#39; &gt;&gt; comparison_jenkins.txt<br>cp -f jenkins_new.txt Jenkins_old.txt<br>if [ -s comparison_jenkins.txt ]; then<br>    sed -i &#39;13 r comma.txt&#39; /opt/aws/amazon-cloudwatch-agent/bin/config.json<br>    while IFS= read -r line; do<br>         echo -e  &quot;{\n  \&quot;file_path\&quot;:\&quot;/var/lib/jenkins/jobs/$line/builds/**\&quot;,\n  \&quot;log_group_name\&quot;: \&quot;Deployment_Logs\&quot;,\n  \&quot;log_stream_name\&quot;: \&quot;$line\&quot;\n},&quot; &gt;&gt;result01.txt<br>    done &lt; comparison_jenkins.txt<br>    sed &#39;$ s/.$//&#39; result01.txt &gt; result02.txt<br>    sed -i &#39;14 r result02.txt&#39; /opt/aws/amazon-cloudwatch-agent/bin/config.json<br>    echo &quot;File not empty&quot;<br>else<br>    echo &quot;File is empty&quot;<br>fi</pre><p>Give the required permission to the config.json to run without issues.</p><p>Run the below command to start the Cloudwatch agent and fetch the config.json file:</p><pre>/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a start<br><br>/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hsgzGuarEDLrNfvEqfEEgw.png" /></figure><p>Your Cloudwatch agent will be started and it will start exporting the deployment logs from the EC2 machine to the Cloudwatch in 2–3 minutes.</p><p>If it doesn’t start exporting the logs to Cloudwatch please check the Cloudwatch agent log file for the issues.</p><pre>/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log</pre><p>Also if you’re making any new changes in the already existing file don’t forget to run the below command to check if the changes are good enough to export the logs to the Cloudwatch:</p><pre>/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2d3eIYCLOtljmrAnnZGo2w.png" /></figure><p>Additionally, you can also use the Cloudwatch agent to create different new metrics for your EC2 instance.</p><p>For any additional information regarding the Cloudwatch agent please follow the reference: <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-on-EC2-Instance.html">https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-on-EC2-Instance.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3cecf10f1901" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>