<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pratik Nalawade</title>
    <description>The latest articles on DEV Community by Pratik Nalawade (@pratik_nalawade).</description>
    <link>https://dev.to/pratik_nalawade</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1812829%2F89cf2a3a-06e9-417b-8d80-7c7860b0b1e7.png</url>
      <title>DEV Community: Pratik Nalawade</title>
      <link>https://dev.to/pratik_nalawade</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pratik_nalawade"/>
    <language>en</language>
    <item>
      <title>Setting Up a Monitoring Stack with Nginx Logging Using Grafana, cAdvisor, Promtail, Prometheus, and Loki</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Thu, 05 Sep 2024 17:59:56 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/setting-up-a-monitoring-stack-with-nginx-logging-using-grafana-cadvisor-promtail-prometheus-and-loki-4pjh</link>
      <guid>https://dev.to/pratik_nalawade/setting-up-a-monitoring-stack-with-nginx-logging-using-grafana-cadvisor-promtail-prometheus-and-loki-4pjh</guid>
      <description>&lt;p&gt;In this guide, I’ll walk you through setting up a comprehensive monitoring and logging stack using Docker and Docker Compose. We'll integrate &lt;strong&gt;Nginx&lt;/strong&gt; logging into &lt;strong&gt;Grafana&lt;/strong&gt;, using &lt;strong&gt;Prometheus&lt;/strong&gt;, &lt;strong&gt;Loki&lt;/strong&gt;, &lt;strong&gt;Promtail&lt;/strong&gt;, and &lt;strong&gt;cAdvisor&lt;/strong&gt;. These tools help you track container resource usage and visualize metrics and logs in real-time.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why This Stack?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Grafana&lt;/strong&gt;: Dashboarding tool for visualizing data from various sources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loki&lt;/strong&gt;: Lightweight log aggregation system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Promtail&lt;/strong&gt;: Agent to ship logs to Loki.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prometheus&lt;/strong&gt;: Monitoring system that collects and processes metrics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;cAdvisor&lt;/strong&gt;: Analyzes and exposes resource usage for running containers.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Step-by-Step Setup
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Install Grafana on Debian/Ubuntu
&lt;/h4&gt;

&lt;p&gt;Install &lt;strong&gt;Grafana&lt;/strong&gt; using the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; apt-transport-https software-properties-common wget
&lt;span class="nb"&gt;sudo &lt;/span&gt;wget &lt;span class="nt"&gt;-q&lt;/span&gt; &lt;span class="nt"&gt;-O&lt;/span&gt; /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main"&lt;/span&gt; | &lt;span class="nb"&gt;sudo tee&lt;/span&gt; /etc/apt/sources.list.d/grafana.list
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;grafana
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start grafana-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify Grafana by opening &lt;code&gt;http://localhost:3000&lt;/code&gt; in your browser.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Install Loki and Promtail Using Docker
&lt;/h4&gt;

&lt;p&gt;First, download the Loki and Promtail configuration files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget https://raw.githubusercontent.com/grafana/loki/v2.8.0/cmd/loki/loki-local-config.yaml &lt;span class="nt"&gt;-O&lt;/span&gt; loki-config.yaml
wget https://raw.githubusercontent.com/grafana/loki/v2.8.0/clients/cmd/promtail/promtail-docker-config.yaml &lt;span class="nt"&gt;-O&lt;/span&gt; promtail-config.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, run the containers for Loki and Promtail:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; loki &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;:/mnt/config &lt;span class="nt"&gt;-p&lt;/span&gt; 3100:3100 grafana/loki:2.8.0 &lt;span class="nt"&gt;--config&lt;/span&gt;.file&lt;span class="o"&gt;=&lt;/span&gt;/mnt/config/loki-config.yaml
docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; promtail &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;:/mnt/config &lt;span class="nt"&gt;-v&lt;/span&gt; /var/log:/var/log &lt;span class="nt"&gt;--link&lt;/span&gt; loki grafana/promtail:2.8.0 &lt;span class="nt"&gt;--config&lt;/span&gt;.file&lt;span class="o"&gt;=&lt;/span&gt;/mnt/config/promtail-config.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Install Prometheus and cAdvisor
&lt;/h4&gt;

&lt;p&gt;To monitor containers with Prometheus and cAdvisor, follow these steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget https://raw.githubusercontent.com/prometheus/prometheus/main/documentation/examples/prometheus.yml &lt;span class="nt"&gt;-O&lt;/span&gt; prometheus.yml
docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;prometheus &lt;span class="nt"&gt;-p&lt;/span&gt; 9090:9090 &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus &lt;span class="nt"&gt;--config&lt;/span&gt;.file&lt;span class="o"&gt;=&lt;/span&gt;/etc/prometheus/prometheus.yml
docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;cadvisor &lt;span class="nt"&gt;-p&lt;/span&gt; 8080:8080 gcr.io/cadvisor/cadvisor:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. Configure Nginx Logging
&lt;/h4&gt;

&lt;p&gt;I set up &lt;strong&gt;Nginx&lt;/strong&gt; to forward its logs to Promtail and visualize them in Grafana. Here's how you can do the same:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Install and run Nginx&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;nginx
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Add Nginx logs to Promtail&lt;/strong&gt;:&lt;br&gt;
Update the Promtail configuration (&lt;code&gt;promtail-config.yaml&lt;/code&gt;) to include the Nginx logs directory, typically &lt;code&gt;/var/log/nginx/*.log&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Restart Promtail&lt;/strong&gt; to apply the new configuration:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker restart promtail
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  5. Set Up Grafana Dashboards
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Add &lt;strong&gt;Prometheus&lt;/strong&gt; as a data source in Grafana.&lt;/li&gt;
&lt;li&gt;Add &lt;strong&gt;Loki&lt;/strong&gt; as a data source for log aggregation.&lt;/li&gt;
&lt;li&gt;Create custom dashboards to visualize Nginx access and error logs, along with container metrics from cAdvisor and Prometheus.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This stack gives you a comprehensive monitoring setup with real-time metrics and logs for your Nginx server and Docker containers. You can monitor resource usage, analyze logs, and even set alerts for performance issues.&lt;/p&gt;

&lt;p&gt;By leveraging Grafana's powerful visualization capabilities, you get full insights into your infrastructure, ensuring everything runs smoothly.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Automating Security Audits and Server Hardening on Linux Servers with Bash Scripting</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Thu, 29 Aug 2024 14:39:41 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/automating-security-audits-and-server-hardening-on-linux-servers-with-bash-scripting-45jl</link>
      <guid>https://dev.to/pratik_nalawade/automating-security-audits-and-server-hardening-on-linux-servers-with-bash-scripting-45jl</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.tourl"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the world of IT and DevOps, security is paramount. Ensuring that Linux servers are secure from vulnerabilities, properly configured, and compliant with industry standards is crucial. This blog explores a Bash script I developed to automate security audits and server hardening on Linux servers, providing a modular, reusable solution that can be easily deployed across multiple environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Overview
&lt;/h2&gt;

&lt;p&gt;The task was to create a comprehensive Bash script that not only audits security settings on a Linux server but also implements necessary hardening measures. This script aims to identify and address common security vulnerabilities, manage user and group permissions, enforce strict file and directory permissions, monitor services, and ensure that the server’s network configuration adheres to best practices.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Components
&lt;/h3&gt;

&lt;h3&gt;
  
  
  1. User and Group Audits
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;: The goal here is to ensure that only authorized users have access to the server, and that their permissions are appropriate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# List all users and groups on the server&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Listing all users:"&lt;/span&gt;
&lt;span class="nb"&gt;cut&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt;: &lt;span class="nt"&gt;-f1&lt;/span&gt; /etc/passwd

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Listing all groups:"&lt;/span&gt;
&lt;span class="nb"&gt;cut&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt;: &lt;span class="nt"&gt;-f1&lt;/span&gt; /etc/group

&lt;span class="c"&gt;# Check for users with UID 0 (root privileges)&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Checking for non-standard users with UID 0:"&lt;/span&gt;
&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;: &lt;span class="s1"&gt;'($3 == "0") {print}'&lt;/span&gt; /etc/passwd

&lt;span class="c"&gt;# Check for users without passwords&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Checking for users without passwords:"&lt;/span&gt;
&lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="nt"&gt;-F&lt;/span&gt;: &lt;span class="s1"&gt;'($2 == "") {print $1}'&lt;/span&gt; /etc/shadow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script lists all users and groups by extracting them from &lt;code&gt;/etc/passwd&lt;/code&gt; and &lt;code&gt;/etc/group&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It identifies users with UID 0 (root privileges) to ensure no unauthorized users have root access.&lt;/li&gt;
&lt;li&gt;The script also checks for users without passwords by inspecting the &lt;code&gt;/etc/shadow&lt;/code&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. File and Directory Permissions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;: Ensure that sensitive files and directories are not accessible by unauthorized users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Scan for world-writable files and directories&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Scanning for world-writable files and directories:"&lt;/span&gt;
find / &lt;span class="nt"&gt;-xdev&lt;/span&gt; &lt;span class="nt"&gt;-type&lt;/span&gt; d &lt;span class="nt"&gt;-perm&lt;/span&gt; &lt;span class="nt"&gt;-0002&lt;/span&gt; &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-ld&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="se"&gt;\;&lt;/span&gt;

&lt;span class="c"&gt;# Check for .ssh directories with secure permissions&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Checking .ssh directories permissions:"&lt;/span&gt;
find /home &lt;span class="nt"&gt;-type&lt;/span&gt; d &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s2"&gt;".ssh"&lt;/span&gt; &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;chmod &lt;/span&gt;700 &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="se"&gt;\;&lt;/span&gt;

&lt;span class="c"&gt;# Report SUID/SGID files&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Checking for SUID/SGID files:"&lt;/span&gt;
find / &lt;span class="nt"&gt;-xdev&lt;/span&gt; &lt;span class="se"&gt;\(&lt;/span&gt; &lt;span class="nt"&gt;-perm&lt;/span&gt; &lt;span class="nt"&gt;-4000&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="nt"&gt;-perm&lt;/span&gt; &lt;span class="nt"&gt;-2000&lt;/span&gt; &lt;span class="se"&gt;\)&lt;/span&gt; &lt;span class="nt"&gt;-type&lt;/span&gt; f &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-ld&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="se"&gt;\;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script scans the entire file system for world-writable files and directories using the &lt;code&gt;find&lt;/code&gt; command, which could pose a security risk.&lt;/li&gt;
&lt;li&gt;It ensures &lt;code&gt;.ssh&lt;/code&gt; directories are secure by setting their permissions to &lt;code&gt;700&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The script also identifies files with SUID/SGID bits set, which could allow unauthorized users to execute files with elevated privileges.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Service Audits
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;: Ensure that only necessary and authorized services are running, and that they are configured securely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# List all running services&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Listing all running services:"&lt;/span&gt;
systemctl list-units &lt;span class="nt"&gt;--type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;service &lt;span class="nt"&gt;--state&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;running

&lt;span class="c"&gt;# Check for unnecessary services&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Checking for unnecessary services:"&lt;/span&gt;
&lt;span class="nv"&gt;UNNECESSARY_SERVICES&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;avahi-daemon cups&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;SERVICE &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;UNNECESSARY_SERVICES&lt;/span&gt;&lt;span class="p"&gt;[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;systemctl is-active &lt;span class="nt"&gt;--quiet&lt;/span&gt; &lt;span class="nv"&gt;$SERVICE&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$SERVICE&lt;/span&gt;&lt;span class="s2"&gt; is running, consider disabling."&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;

&lt;span class="c"&gt;# Ensure critical services are running&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Checking critical services:"&lt;/span&gt;
&lt;span class="nv"&gt;CRITICAL_SERVICES&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;sshd iptables&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;SERVICE &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;CRITICAL_SERVICES&lt;/span&gt;&lt;span class="p"&gt;[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  &lt;/span&gt;systemctl is-active &lt;span class="nt"&gt;--quiet&lt;/span&gt; &lt;span class="nv"&gt;$SERVICE&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$SERVICE&lt;/span&gt;&lt;span class="s2"&gt; is not running!"&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script lists all running services using &lt;code&gt;systemctl&lt;/code&gt; and checks for any unnecessary ones.&lt;/li&gt;
&lt;li&gt;It verifies that critical services like &lt;code&gt;sshd&lt;/code&gt; and &lt;code&gt;iptables&lt;/code&gt; are running, ensuring that the server’s basic security mechanisms are in place.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Firewall and Network Security
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;: Verify that the server’s firewall is configured correctly and that there are no insecure network settings.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Verify if the firewall is active&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Checking if firewall is active:"&lt;/span&gt;
ufw status | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-qw&lt;/span&gt; &lt;span class="s2"&gt;"active"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Firewall is not active!"&lt;/span&gt;

&lt;span class="c"&gt;# Report open ports and services&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Listing open ports:"&lt;/span&gt;
netstat &lt;span class="nt"&gt;-tuln&lt;/span&gt;

&lt;span class="c"&gt;# Check for IP forwarding&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Checking for IP forwarding:"&lt;/span&gt;
sysctl net.ipv4.ip_forward
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script checks if the firewall (&lt;code&gt;ufw&lt;/code&gt;) is active and configured properly.&lt;/li&gt;
&lt;li&gt;It lists all open ports and their associated services using &lt;code&gt;netstat&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The script also checks if IP forwarding is enabled, which could expose the server to security risks if not required.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. IP and Network Configuration Checks
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;: Ensure that the server’s IP addresses are properly configured and identified as public or private.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# List all IP addresses and classify them&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Listing all IP addresses:"&lt;/span&gt;
ip &lt;span class="nt"&gt;-o&lt;/span&gt; addr show | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $2, $4}'&lt;/span&gt;

&lt;span class="c"&gt;# Identify public vs private IPs&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Identifying public vs private IP addresses:"&lt;/span&gt;
ip &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="nt"&gt;-4&lt;/span&gt; addr list | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $4}'&lt;/span&gt; | &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nb"&gt;read &lt;/span&gt;IP&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do
  if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="nv"&gt;$IP&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;~ ^10&lt;span class="se"&gt;\.&lt;/span&gt;|^172&lt;span class="se"&gt;\.&lt;/span&gt;16|^192&lt;span class="se"&gt;\.&lt;/span&gt;168 &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$IP&lt;/span&gt;&lt;span class="s2"&gt; is private."&lt;/span&gt;
  &lt;span class="k"&gt;else
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$IP&lt;/span&gt;&lt;span class="s2"&gt; is public."&lt;/span&gt;
  &lt;span class="k"&gt;fi
done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script lists all IP addresses assigned to the server using the &lt;code&gt;ip&lt;/code&gt; command.&lt;/li&gt;
&lt;li&gt;It then classifies each IP address as public or private, providing a clear understanding of the server’s network exposure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. Security Updates and Patching
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;: Ensure the server is up-to-date with the latest security patches.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Check for available updates&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Checking for available updates:"&lt;/span&gt;
apt-get update &lt;span class="nt"&gt;-qq&lt;/span&gt;
apt-get upgrade &lt;span class="nt"&gt;-s&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s2"&gt;"security"&lt;/span&gt;

&lt;span class="c"&gt;# Ensure automatic security updates are enabled&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Ensuring automatic security updates are enabled:"&lt;/span&gt;
dpkg-reconfigure &lt;span class="nt"&gt;--priority&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;low unattended-upgrades
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script checks for available security updates using &lt;code&gt;apt-get&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It ensures that automatic security updates are enabled, reducing the risk of the server being compromised due to outdated software.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  7. Log Monitoring
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;: Detect any suspicious activity that could indicate a security breach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Check for suspicious log entries&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Checking for suspicious log entries:"&lt;/span&gt;
&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"Failed password"&lt;/span&gt; /var/log/auth.log | &lt;span class="nb"&gt;tail&lt;/span&gt; &lt;span class="nt"&gt;-10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script searches the authentication log (&lt;code&gt;/var/log/auth.log&lt;/code&gt;) for failed login attempts, which could indicate brute-force attacks or unauthorized access attempts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  8. Server Hardening Steps
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;: Implement security best practices to harden the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Disable root login via SSH&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Disabling root login via SSH:"&lt;/span&gt;
&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/PermitRootLogin yes/PermitRootLogin no/'&lt;/span&gt; /etc/ssh/sshd_config
systemctl reload sshd

&lt;span class="c"&gt;# Disable IPv6 if not required&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Disabling IPv6:"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"net.ipv6.conf.all.disable_ipv6 = 1"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /etc/sysctl.conf
sysctl &lt;span class="nt"&gt;-p&lt;/span&gt;

&lt;span class="c"&gt;# Secure GRUB with a password&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Securing GRUB with a password:"&lt;/span&gt;
grub2-mkpasswd-pbkdf2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script disables root login via SSH to prevent unauthorized access.&lt;/li&gt;
&lt;li&gt;It disables IPv6 if not required, reducing the server’s attack surface.&lt;/li&gt;
&lt;li&gt;The script also secures the GRUB bootloader with a password to prevent unauthorized changes to boot parameters.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  9. Custom Security Checks
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;: Allow customization of the script to meet specific security policies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Example custom check: Ensure no files have '777' permissions&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Checking for files with 777 permissions:"&lt;/span&gt;
find / &lt;span class="nt"&gt;-type&lt;/span&gt; f &lt;span class="nt"&gt;-perm&lt;/span&gt; 0777 &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="se"&gt;\;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script includes an example of a custom security check, ensuring that no files have overly permissive &lt;code&gt;777&lt;/code&gt; permissions.&lt;/li&gt;
&lt;li&gt;This modular design allows users to easily add or modify security checks according to their organization’s policies.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  10. Reporting and Alerting
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;: Provide a comprehensive summary report and optional alerts for critical issues.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Generate summary report&lt;/span&gt;
&lt;span class="nv"&gt;REPORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/var/log/security_audit_report.txt"&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Generating security audit report..."&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Security Audit Report"&lt;/span&gt;
  &lt;span class="nb"&gt;date
  echo&lt;/span&gt; &lt;span class="s2"&gt;"---------------------------------"&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"User and Group Audits:"&lt;/span&gt;
  &lt;span class="c"&gt;# Include results of user and group audits&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"File and Directory Permissions:"&lt;/span&gt;
  &lt;span class="c"&gt;# Include results of permission checks&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Service Audits:"&lt;/span&gt;
  &lt;span class="c"&gt;# Include results of service audits&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Firewall and Network Security:"&lt;/span&gt;
  &lt;span class="c"&gt;# Include results of firewall and network checks&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"IP and Network Configuration:"&lt;/span&gt;
  &lt;span class="c"&gt;# Include results of IP checks&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$REPORT&lt;/span&gt;

&lt;span class="c"&gt;# Send email alert if critical issues found&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Sending email alert if necessary..."&lt;/span&gt;
&lt;span class="c"&gt;# Example: Send an alert if a critical issue is found&lt;/span&gt;
&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"CRITICAL"&lt;/span&gt; &lt;span class="nv"&gt;$REPORT&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; mail &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="s2"&gt;"Security Audit Alert"&lt;/span&gt; admin@example.com &amp;lt; &lt;span class="nv"&gt;$REPORT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**&lt;/p&gt;

&lt;p&gt;Explanation**:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script generates a structured summary report that includes the results of all security checks and hardening steps.&lt;/li&gt;
&lt;li&gt;If any critical issues are detected, the script can send an email alert to the administrator.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Challenges and Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Dependency Management
&lt;/h3&gt;

&lt;p&gt;One of the initial challenges was ensuring that all necessary tools and dependencies were installed on the server. For example, tools like &lt;code&gt;netstat&lt;/code&gt; for network monitoring and &lt;code&gt;chkrootkit&lt;/code&gt; for rootkit detection had to be installed manually if not already present. To resolve this, the script checks for required packages and installs them as needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Permission Issues
&lt;/h3&gt;

&lt;p&gt;Running the script without appropriate permissions led to errors, particularly when modifying system files or changing configurations. This was resolved by running the script with &lt;code&gt;sudo&lt;/code&gt;, ensuring that it had the necessary privileges to execute all commands.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modular Design
&lt;/h3&gt;

&lt;p&gt;Creating a modular script that could be easily extended or customized was another challenge. To achieve this, each audit or hardening step was encapsulated in its function, allowing for independent execution and easy updates. This approach made the script more maintainable and adaptable to different server environments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Public vs. Private IP Identification
&lt;/h3&gt;

&lt;p&gt;Identifying public versus private IP addresses required careful handling of the server’s network interfaces. By leveraging &lt;code&gt;ip&lt;/code&gt; commands and regular expressions, the script accurately categorized each IP address and provided a clear summary in the final report.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reporting and Alerting
&lt;/h3&gt;

&lt;p&gt;Generating a comprehensive and readable report was essential for ensuring that the security audit results were actionable. The script formats the output into a structured report, with critical findings highlighted. Additionally, integrating email alerts for critical issues ensures that administrators are promptly notified of any urgent vulnerabilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use the Script
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Ensure that your server is running a Linux distribution (e.g., Ubuntu, CentOS).&lt;/li&gt;
&lt;li&gt;Install necessary packages like &lt;code&gt;net-tools&lt;/code&gt;, &lt;code&gt;chkrootkit&lt;/code&gt;, &lt;code&gt;ufw&lt;/code&gt;, and &lt;code&gt;unattended-upgrades&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Clone the script from the GitHub repository and give it executable permissions using &lt;code&gt;chmod +x&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Running the Script
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Full Audit and Hardening&lt;/strong&gt;: To run the full audit and hardening process, simply execute the script with sudo privileges:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo&lt;/span&gt; ./security_audit_hardening.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Custom Checks&lt;/strong&gt;: If you wish to run specific checks or hardening steps, modify the script’s configuration file to enable or disable particular modules.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reviewing Reports&lt;/strong&gt;: After execution, the script will generate a summary report in the specified output directory. Review this report for any vulnerabilities or issues that require attention.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Email Alerts&lt;/strong&gt;: If configured, email alerts will be sent automatically for any critical findings.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This Bash script offers a powerful, automated solution for conducting security audits and hardening Linux servers. By addressing common vulnerabilities, enforcing strict security measures, and providing detailed reporting, it helps ensure that your servers remain secure and compliant with industry standards.&lt;/p&gt;

&lt;p&gt;Check out the complete script and documentation on my GitHub![&lt;a href="https://github.com/PRATIKNALAWADE/Audit-ServerHardening" rel="noopener noreferrer"&gt;https://github.com/PRATIKNALAWADE/Audit-ServerHardening&lt;/a&gt;]&lt;/p&gt;

</description>
      <category>linux</category>
      <category>devops</category>
      <category>bash</category>
      <category>automation</category>
    </item>
    <item>
      <title>Building a Real-Time System Monitoring Dashboard with Bash</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Tue, 27 Aug 2024 09:55:11 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/building-a-real-time-system-monitoring-dashboard-with-bash-5d98</link>
      <guid>https://dev.to/pratik_nalawade/building-a-real-time-system-monitoring-dashboard-with-bash-5d98</guid>
      <description>&lt;p&gt;In today’s fast-paced IT environments, monitoring system resources in real time is crucial for maintaining the health and performance of servers. Whether you’re managing a proxy server or any other critical infrastructure, having a lightweight, customizable monitoring solution can make a big difference. In this blog post, I’ll walk you through how to create a real-time system monitoring dashboard using a Bash script. This script will allow you to keep an eye on CPU usage, network activity, disk space, and more—all from your terminal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Bash?
&lt;/h2&gt;

&lt;p&gt;Bash is a powerful and flexible tool that comes pre-installed on most Linux systems. While there are many sophisticated monitoring tools available, Bash offers a simple, no-frills approach that’s easy to customize and deploy. Plus, it’s a great way to deepen your understanding of system resources and shell scripting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Features of the Script
&lt;/h2&gt;

&lt;p&gt;The monitoring automation script we’ll build includes the following features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Top 10 Most Used Applications&lt;/strong&gt;: Displays the top 10 processes consuming the most CPU and memory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network Monitoring&lt;/strong&gt;: Tracks the number of concurrent connections, packet drops, and data transfer rates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Disk Usage&lt;/strong&gt;: Monitors disk space usage, highlighting partitions using more than 80% of their capacity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System Load&lt;/strong&gt;: Shows the current load average and a detailed breakdown of CPU usage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory Usage&lt;/strong&gt;: Provides an overview of total, used, and free memory, including swap usage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Process Monitoring&lt;/strong&gt;: Lists the number of active processes and the top 5 resource-intensive ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Service Monitoring&lt;/strong&gt;: Checks the status of essential services like &lt;code&gt;sshd&lt;/code&gt;, &lt;code&gt;nginx/apache&lt;/code&gt;, and &lt;code&gt;iptables&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom Dashboard&lt;/strong&gt;: Allows users to view specific sections of the dashboard using command-line switches.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Building the Script
&lt;/h2&gt;

&lt;p&gt;Let’s dive into how we can build this script. Below is a breakdown of each section.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Monitoring Top Applications
&lt;/h3&gt;

&lt;p&gt;To display the top 10 applications consuming the most CPU and memory, we use the &lt;code&gt;ps&lt;/code&gt; command combined with &lt;code&gt;sort&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ps aux &lt;span class="nt"&gt;--sort&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;-%cpu | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 11
ps aux &lt;span class="nt"&gt;--sort&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;-%mem | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This provides a quick overview of the most resource-hungry processes.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Network Monitoring
&lt;/h3&gt;

&lt;p&gt;For network monitoring, we need to track concurrent connections, packet drops, and data transfer rates. We use &lt;code&gt;netstat&lt;/code&gt; and &lt;code&gt;ifconfig&lt;/code&gt; for this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;netstat &lt;span class="nt"&gt;-an&lt;/span&gt; | &lt;span class="nb"&gt;grep &lt;/span&gt;ESTABLISHED | &lt;span class="nb"&gt;wc&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;
ifconfig eth0 | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s1"&gt;'RX packets'&lt;/span&gt;
ifconfig eth0 | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s1"&gt;'TX packets'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These commands give us insights into the network activity in real-time.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Disk Usage
&lt;/h3&gt;

&lt;p&gt;Monitoring disk usage is crucial for preventing storage issues. Here’s how we can highlight partitions using more than 80% of disk space:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;df&lt;/span&gt; &lt;span class="nt"&gt;-h&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'$5 &amp;gt; 80 {print $0}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This snippet checks the disk usage and flags any partitions that exceed the 80% threshold.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. System Load
&lt;/h3&gt;

&lt;p&gt;To monitor the system load, we use the &lt;code&gt;uptime&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;uptime&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command gives a snapshot of the load average, which indicates how many processes are demanding CPU time.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Memory Usage
&lt;/h3&gt;

&lt;p&gt;Memory usage is tracked using &lt;code&gt;free&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;free &lt;span class="nt"&gt;-h&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This provides a detailed look at total, used, and free memory, including swap space.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Process Monitoring
&lt;/h3&gt;

&lt;p&gt;We can easily monitor active processes and identify the top 5 resource-intensive ones:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ps &lt;span class="nt"&gt;-e&lt;/span&gt; | &lt;span class="nb"&gt;wc&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;
ps aux &lt;span class="nt"&gt;--sort&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;-%cpu | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This combination helps in understanding the process load on the system.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Service Monitoring
&lt;/h3&gt;

&lt;p&gt;Ensuring essential services are running is vital. We can check the status of critical services like &lt;code&gt;sshd&lt;/code&gt; and &lt;code&gt;iptables&lt;/code&gt; using &lt;code&gt;systemctl&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;systemctl is-active sshd
systemctl is-active iptables
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These checks ensure that critical services are operational and correctly configured.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Custom Dashboard
&lt;/h3&gt;

&lt;p&gt;Finally, to make the script more flexible, we add command-line switches to allow users to view specific parts of the dashboard:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="nt"&gt;-cpu&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; monitor_cpu &lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="nt"&gt;-memory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; monitor_memory &lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="nt"&gt;-network&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; monitor_network &lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="nt"&gt;-disk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; monitor_disk &lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Usage: &lt;/span&gt;&lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="s2"&gt; {-cpu|-memory|-network|-disk}"&lt;/span&gt; &lt;span class="p"&gt;;;&lt;/span&gt;
&lt;span class="k"&gt;esac&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This modular approach makes it easy to focus on the metrics that matter most at any given time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;p&gt;While building this script, I encountered a few challenges. For example, I initially forgot to install &lt;code&gt;net-tools&lt;/code&gt;, which includes the &lt;code&gt;netstat&lt;/code&gt; command necessary for network monitoring. This was quickly resolved by installing the package via &lt;code&gt;sudo apt-get install net-tools&lt;/code&gt;. Additionally, ensuring that the script was executable required setting the correct permissions with &lt;code&gt;chmod +x&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This Bash script provides a powerful, lightweight way to monitor system resources in real-time. It’s easy to customize and deploy, making it a valuable tool for anyone managing Linux servers. Whether you’re a seasoned sysadmin or just getting started with shell scripting, this project offers a hands-on way to deepen your understanding of system monitoring.&lt;/p&gt;

&lt;p&gt;You can find the full script and documentation in my &lt;a href="https://github.com/PRATIKNALAWADE/SystemMonitoring" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;. Feel free to clone, modify, and use it as a starting point for your own monitoring solutions.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>DevOps Monitoring Project</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Sat, 24 Aug 2024 14:39:48 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/devops-monitoring-project-4ipo</link>
      <guid>https://dev.to/pratik_nalawade/devops-monitoring-project-4ipo</guid>
      <description>&lt;h1&gt;
  
  
  Learning DevOps Monitoring with DevOps Shack: A Hands-On Journey
&lt;/h1&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7n4h0nw5k5hcjgtvh434.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7n4h0nw5k5hcjgtvh434.png" alt="Image description" width="800" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the ever-evolving world of DevOps, monitoring is a critical aspect of maintaining the health and performance of your infrastructure. Recently, I embarked on a hands-on learning journey, following along with a YouTuber named &lt;strong&gt;DevOps Shack&lt;/strong&gt;. Through their comprehensive tutorials, I implemented a full-fledged monitoring solution using Prometheus, Node Exporter, Alertmanager, and Blackbox Exporter. This blog post shares my experience and the key takeaways from the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Overview
&lt;/h2&gt;

&lt;p&gt;The project is designed to provide an end-to-end monitoring solution for your infrastructure. By following the guidance of DevOps Shack, I was able to set up a robust system that not only monitors the health of virtual machines but also sends alerts for critical issues and probes service availability.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tools and Technologies
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prometheus&lt;/strong&gt;: Used for collecting and storing metrics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Node Exporter&lt;/strong&gt;: Used to expose hardware and OS metrics to Prometheus.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alertmanager&lt;/strong&gt;: Manages alerts generated by Prometheus.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Blackbox Exporter&lt;/strong&gt;: Probes endpoints to check their availability.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Before getting started, I ensured that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two virtual machines (VMs) were prepared.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wget&lt;/code&gt; and &lt;code&gt;tar&lt;/code&gt; were installed on both VMs.&lt;/li&gt;
&lt;li&gt;I had the necessary permissions to download, extract, and run the binaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step-by-Step Setup
&lt;/h2&gt;

&lt;h3&gt;
  
  
  VM-1: Setting Up Node Exporter
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Download Node Exporter:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget https://github.com/prometheus/node_exporter/releases/download/v1.8.1/node_exporter-1.8.1.linux-amd64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Extract Node Exporter:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;tar &lt;/span&gt;xvfz node_exporter-1.8.1.linux-amd64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Start Node Exporter:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;node_exporter-1.8.1.linux-amd64
./node_exporter &amp;amp;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  VM-2: Setting Up Prometheus, Alertmanager, and Blackbox Exporter
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Prometheus Setup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Download Prometheus:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget https://github.com/prometheus/prometheus/releases/download/v2.52.0/prometheus-2.52.0.linux-amd64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Extract Prometheus:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;tar &lt;/span&gt;xvfz prometheus-2.52.0.linux-amd64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Start Prometheus:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;prometheus-2.52.0.linux-amd64
./prometheus &lt;span class="nt"&gt;--config&lt;/span&gt;.file&lt;span class="o"&gt;=&lt;/span&gt;prometheus.yml &amp;amp;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Alertmanager Setup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Download Alertmanager:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Extract Alertmanager:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;tar &lt;/span&gt;xvfz alertmanager-0.27.0.linux-amd64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Start Alertmanager:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;alertmanager-0.27.0.linux-amd64
./alertmanager &lt;span class="nt"&gt;--config&lt;/span&gt;.file&lt;span class="o"&gt;=&lt;/span&gt;alertmanager.yml &amp;amp;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Blackbox Exporter Setup&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Download Blackbox Exporter:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.25.0/blackbox_exporter-0.25.0.linux-amd64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Extract Blackbox Exporter:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;tar &lt;/span&gt;xvfz blackbox_exporter-0.25.0.linux-amd64.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Start Blackbox Exporter:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;blackbox_exporter-0.25.0.linux-amd64
./blackbox_exporter &amp;amp;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuration Details
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fus1tmfg6k0xnv9iqvboz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fus1tmfg6k0xnv9iqvboz.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prometheus Configuration (&lt;code&gt;prometheus.yml&lt;/code&gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Global Configuration:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scrape interval: &lt;code&gt;15s&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Evaluation interval: &lt;code&gt;15s&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Scrape Configurations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prometheus itself:&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;Job name: &lt;code&gt;prometheus&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Target: &lt;code&gt;localhost:9090&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Node Exporter:&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;Job name: &lt;code&gt;node_exporter&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Target: &lt;code&gt;3.110.195.114:9100&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Blackbox Exporter:&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;Job name: &lt;code&gt;blackbox&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Targets: 

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;http://prometheus.io&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;https://prometheus.io&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;http://3.110.195.114:8080/&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Alertmanager Configuration (&lt;code&gt;alertmanager.yml&lt;/code&gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Routing Configuration:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Group alerts by: &lt;code&gt;alertname&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Group wait: &lt;code&gt;30s&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Group interval: &lt;code&gt;5m&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Repeat interval: &lt;code&gt;1h&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Default receiver: &lt;code&gt;email-notifications&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Receiver Configuration:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Receiver name: &lt;code&gt;email-notifications&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Email recipient: &lt;code&gt;email@gmail.com&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;SMTP server: &lt;code&gt;smtp.gmail.com:587&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Auth username and password (to be configured).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Inhibition Rules:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Source match: &lt;code&gt;severity: critical&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Target match: &lt;code&gt;severity: warning&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Equal fields: &lt;code&gt;alertname&lt;/code&gt;, &lt;code&gt;dev&lt;/code&gt;, &lt;code&gt;instance&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Alert Rules Configuration (&lt;code&gt;alert_rules.yml&lt;/code&gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some of the key alert rules I configured include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;InstanceDown&lt;/strong&gt;: Alerts if an instance is down for more than 1 minute.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WebsiteDown&lt;/strong&gt;: Alerts if a website probe fails.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HostOutOfMemory&lt;/strong&gt;: Alerts if memory availability drops below 25%.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HostOutOfDiskSpace&lt;/strong&gt;: Alerts if disk space is less than 50%.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HostHighCpuLoad&lt;/strong&gt;: Alerts if CPU load exceeds 80%.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ServiceUnavailable&lt;/strong&gt;: Alerts if a service is unavailable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HighMemoryUsage&lt;/strong&gt;: Alerts if memory usage exceeds 90%.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FileSystemFull&lt;/strong&gt;: Alerts if file system free space drops below 10%.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft50alpid6oydwwejv9xg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft50alpid6oydwwejv9xg.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fni4q4xdckbfvle2el2kx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fni4q4xdckbfvle2el2kx.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foaucvkmu3tzak2k18562.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foaucvkmu3tzak2k18562.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Firewall and Security Settings
&lt;/h3&gt;

&lt;p&gt;I had to configure the firewall to allow traffic on the necessary ports:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prometheus: &lt;code&gt;9090&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Alertmanager: &lt;code&gt;9093&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Blackbox Exporter: &lt;code&gt;9115&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Node Exporter: &lt;code&gt;9100&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key Features and Functionalities
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Monitoring&lt;/strong&gt;: Using Node Exporter and Prometheus, I was able to monitor crucial system metrics such as CPU usage, memory availability, and disk space. These metrics are scraped at regular intervals, providing real-time insights into the system's performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Alerting&lt;/strong&gt;: With Alertmanager, I was able to set up notifications for critical events, ensuring that I was immediately informed of any issues, such as an instance going down or high CPU load. The flexibility of Alertmanager's configuration allowed me to tailor alerts to meet specific needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Probing&lt;/strong&gt;: The Blackbox Exporter allowed me to monitor the availability and response times of various endpoints, including web services, ensuring that they remained accessible and responsive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges and Solutions
&lt;/h2&gt;

&lt;p&gt;One of the challenges I faced during this project was configuring the firewall and ensuring proper communication between the services across the two VMs. By carefully adjusting firewall rules and thoroughly reviewing the configuration files, I was able to overcome these hurdles and achieve a seamless setup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Future Enhancements
&lt;/h2&gt;

&lt;p&gt;Moving forward, I plan to enhance this setup by integrating Grafana for more sophisticated visualization of the metrics collected by Prometheus. Additionally, I am considering automating the entire deployment process using Ansible, making it easier to replicate the setup across different environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Following along with DevOps Shack's tutorials provided me with a solid foundation in setting up a comprehensive monitoring and alerting system using open-source tools. This project, &lt;strong&gt;DevOps Shack&lt;/strong&gt;, serves as a testament to the power of hands-on learning in mastering DevOps concepts. I encourage anyone interested in DevOps to explore these tools, experiment with configurations, and discover the power of effective monitoring in maintaining a healthy infrastructure.&lt;/p&gt;

&lt;p&gt;You can explore more about the project on DevOps Shack’s YouTube channel (insert the actual link). If you have any questions or feedback, feel free to reach out!&lt;/p&gt;




&lt;p&gt;This revised version emphasizes your learning experience and the value of following DevOps Shack's guidance. Feel free to personalize it further before publishing!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>prometheus</category>
      <category>learning</category>
      <category>monitoring</category>
    </item>
    <item>
      <title>Shell scripting Interview Questions</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Mon, 19 Aug 2024 10:31:23 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/shell-scripting-interview-questions-16ff</link>
      <guid>https://dev.to/pratik_nalawade/shell-scripting-interview-questions-16ff</guid>
      <description>&lt;p&gt;Most asked shell scripting and Linux interview questions along with their answers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;What is a shell script?&lt;br&gt;
— Answer: A shell script is a text file containing a sequence of commands that are executed by the shell interpreter. It allows automation of repetitive tasks and execution of multiple commands in sequence.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Differentiate between a shell and a terminal.&lt;br&gt;
— Answer: A shell is a command-line interpreter that interprets user commands and executes them. A terminal is a user interface that provides access to the shell. Multiple terminals can run concurrently, each running its own instance of the shell.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Explain the difference between absolute and relative paths.&lt;br&gt;
— Answer: An absolute path specifies the location of a file or directory from the root directory (/) of the file system. A relative path specifies the location of a file or directory relative to the current working directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is the shebang (#!) line in a shell script?&lt;br&gt;
— Answer: The shebang line (#!) is a special line at the beginning of a shell script that indicates the path to the shell interpreter that should be used to execute the script. For example, &lt;code&gt;#!/bin/bash&lt;/code&gt; specifies that the Bash shell should be used.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How do you comment in a shell script?&lt;br&gt;
— Answer: Comments in shell scripts are preceded by the &lt;code&gt;#&lt;/code&gt; symbol. Anything after the &lt;code&gt;#&lt;/code&gt; symbol on a line is considered a comment and is ignored by the shell.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is the difference between $ and $$ in shell scripting?&lt;br&gt;
— Answer: In shell scripting, &lt;code&gt;$&lt;/code&gt; is used to reference the value of a variable, whereas &lt;code&gt;$$&lt;/code&gt; represents the process ID (PID) of the current shell.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Explain the difference between the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt; operators in shell scripting.&lt;br&gt;
— Answer: The &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator is used to execute the command following it only if the preceding command succeeds (returns a zero exit status). The &lt;code&gt;||&lt;/code&gt; operator is used to execute the command following it only if the preceding command fails (returns a non-zero exit status).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How do you pass arguments to a shell script?&lt;br&gt;
— Answer: Arguments can be passed to a shell script as command-line arguments. These arguments can be accessed within the script using special variables such as &lt;code&gt;$1&lt;/code&gt;, &lt;code&gt;$2&lt;/code&gt;, etc., representing the first, second, etc., arguments respectively.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is process substitution in shell scripting?&lt;br&gt;
— Answer: Process substitution is a feature of some shells (such as Bash) that allows the output of a command or commands to be used as input to another command or commands. It is represented by the &lt;code&gt;&amp;lt;()&lt;/code&gt; and &lt;code&gt;&amp;gt;()&lt;/code&gt; syntax.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Explain the &lt;code&gt;grep&lt;/code&gt; command in Linux.&lt;br&gt;
— Answer: The &lt;code&gt;grep&lt;/code&gt; command is used to search for patterns in text files or streams. It outputs lines that match a specified pattern or regular expression.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is the purpose of the &lt;code&gt;awk&lt;/code&gt; command in Linux?&lt;br&gt;
— Answer: The &lt;code&gt;awk&lt;/code&gt; command is a powerful text processing tool used for pattern scanning and processing. It processes input lines based on patterns and performs actions defined by the user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Explain the purpose of the &lt;code&gt;tar&lt;/code&gt; command in Linux.&lt;br&gt;
— Answer: The &lt;code&gt;tar&lt;/code&gt; command is used to create, view, and extract archives (tarballs) containing multiple files. It is often used for packaging files and directories for distribution or backup purposes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is SSH and how is it used in Linux?&lt;br&gt;
— Answer: SSH (Secure Shell) is a cryptographic network protocol used for secure remote access to Linux systems. It provides encrypted communication between the client and the server, allowing users to log in and execute commands on remote machines securely.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How do you check system resource usage in Linux?&lt;br&gt;
— Answer: System resource usage can be checked using commands such as &lt;code&gt;top&lt;/code&gt;, &lt;code&gt;htop&lt;/code&gt;, &lt;code&gt;free&lt;/code&gt;, and &lt;code&gt;df&lt;/code&gt;. These commands provide information about CPU usage, memory usage, and disk space usage respectively.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These questions cover a range of fundamental concepts and practical skills related to shell scripting and Linux system administration, which are commonly assessed in interviews for DevOps and system administration roles.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Shell script count no. of s in mississippi&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;x="mississippi"&lt;br&gt;
grep -o "s" &amp;lt;&amp;lt;&amp;lt;"$x" | wc -l&lt;/p&gt;

&lt;p&gt;explanation:&lt;br&gt;
grep -o “s” &amp;lt;&amp;lt;&amp;lt;”$x”:&lt;/p&gt;

&lt;p&gt;grep: This command is used to search for patterns in text.&lt;br&gt;
-o: This option tells grep to only output the parts of the text that match the pattern.&lt;/p&gt;

&lt;p&gt;“s”: This is the pattern we’re searching for, in this case, the letter “s”.&lt;br&gt;
&amp;lt;&amp;lt;&amp;lt;”$x”: This feeds the value of the variable $x as input to grep. In simple terms, it’s like saying “search for the letter ‘s’ in the text stored in the variable $x”.&lt;/p&gt;

&lt;p&gt;| (pipe symbol): This symbol is used to pass the output of one command as the input to another command. It’s like connecting two commands together in a pipeline.&lt;/p&gt;

&lt;p&gt;wc -l: wc: Stands for word count. It’s used to count lines, words, and characters in text.&lt;br&gt;
-l: This option tells wc to count only the lines in the input text. In this case, it counts the number of lines.&lt;/p&gt;

&lt;p&gt;Putting it all together, the command grep -o “s” &amp;lt;&amp;lt;&amp;lt;”$x” | wc -l first searches for the letter “s” in the text stored in the variable $x, then it counts the number of lines in the output, which corresponds to the number of times the letter “s” appears in the text.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;crontab and job scheduling&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;crontab&lt;/code&gt; is a command used in Unix-like operating systems to schedule jobs or commands to run periodically at fixed times, dates, or intervals. Here’s an example of how to use &lt;code&gt;crontab&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;Let’s say you want to schedule a script to run every day at 3:00 AM.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your terminal.&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;crontab -e&lt;/code&gt; and press Enter. This command opens the crontab editor.&lt;/li&gt;
&lt;li&gt;If it’s your first time running &lt;code&gt;crontab -e&lt;/code&gt;, it might prompt you to choose an editor. Select your preferred editor (e.g., nano, vim).&lt;/li&gt;
&lt;li&gt;Once the editor opens, add a new line at the end of the file with the following format:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;m h * * * /path/to/your/script.sh&lt;br&gt;
Replace &lt;code&gt;/path/to/your/script.sh&lt;/code&gt; with the actual path to your script.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;m&lt;/code&gt; stands for the minute (0–59).
— &lt;code&gt;h&lt;/code&gt; stands for the hour (0–23).
— &lt;code&gt;*&lt;/code&gt; represents all possible values for that field.
— So &lt;code&gt;* *&lt;/code&gt; means every minute of every hour.
— Finally, the full line means “run the script at every hour (0–23) and minute (0–59) of every day of every month and every day of the week”.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, if you want your script to run every day at 3:00 AM, you would set it up like this:&lt;/p&gt;

&lt;p&gt;0 3 * * * /path/to/your/script.sh&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Save and close the crontab editor. In nano, you can do this by pressing Ctrl + O to write the file and then Ctrl + X to exit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You’ve now set up your cron job. It will run your script at 3:00 AM every day.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Remember, cron uses 24-hour time format, so 3:00 AM is represented as 3. If you wanted to run the script every Sunday at 3:00 AM, you would modify the cron job like this:&lt;/p&gt;

&lt;p&gt;0 3 * * 0 /path/to/your/script.sh&lt;br&gt;
In this case, &lt;code&gt;0&lt;/code&gt; in the fifth field represents Sunday.&lt;/p&gt;

&lt;p&gt;That’s a basic example of how to use &lt;code&gt;crontab&lt;/code&gt; to schedule tasks in Unix-like systems.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Loops and conditionals in shell&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Below are examples of a shell script demonstrating the usage of &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else-if&lt;/code&gt;, and &lt;code&gt;for&lt;/code&gt; loop constructs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else-if&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; statements:&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  !/bin/bash
&lt;/h1&gt;

&lt;h1&gt;
  
  
  1. Prompt user to enter a number
&lt;/h1&gt;

&lt;p&gt;echo "Enter a number:"&lt;br&gt;
read num&lt;/p&gt;

&lt;h1&gt;
  
  
  Check if the number is positive, negative, or zero
&lt;/h1&gt;

&lt;p&gt;if [ $num -gt 0 ]; then&lt;br&gt;
 echo "The number is positive."&lt;br&gt;
elif [ $num -lt 0 ]; then&lt;br&gt;
 echo "The number is negative."&lt;br&gt;
else&lt;br&gt;
 echo "The number is zero."&lt;br&gt;
fi&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Using a &lt;code&gt;for&lt;/code&gt; loop to iterate over a list of items:**
&lt;/h1&gt;

&lt;h1&gt;
  
  
  !/bin/bash
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Define a list of fruits
&lt;/h1&gt;

&lt;p&gt;fruits=("Apple" "Banana" "Orange" "Grapes" "Watermelon")&lt;/p&gt;

&lt;h1&gt;
  
  
  Iterate over the list of fruits using a for loop
&lt;/h1&gt;

&lt;p&gt;echo "List of fruits:"&lt;br&gt;
for fruit in "${fruits[@]}"&lt;br&gt;
do&lt;br&gt;
 echo "$fruit"&lt;br&gt;
done&lt;br&gt;
In the first script, the user is prompted to enter a number, and then the script checks whether the number is positive, negative, or zero using &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; statements.&lt;/p&gt;

&lt;p&gt;In the second script, a list of fruits is defined, and a &lt;code&gt;for&lt;/code&gt; loop is used to iterate over each item in the list and print it to the console.&lt;/p&gt;

&lt;p&gt;These examples demonstrate the basic usage of &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else-if&lt;/code&gt;, &lt;code&gt;else&lt;/code&gt;, and &lt;code&gt;for&lt;/code&gt; loop constructs in shell scripting.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Shell script to print only process ids of all processes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;ps -ef | awk -F " " '{print $2}'&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Hard link vs soft link in linux&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Linux, “hard” and “soft” links are two types of links used to point to files. Here’s a brief explanation of each along with their respective syntax:&lt;/p&gt;

&lt;p&gt;Hard Link:&lt;br&gt;
— A hard link is a direct pointer to the inode (metadata) of a file. It essentially creates a new directory entry that refers to the same underlying data as the original file.&lt;br&gt;
— Changes made to the original file will affect all hard links pointing to it, as they all reference the same data blocks.&lt;br&gt;
— Hard links cannot be created for directories.&lt;br&gt;
— Hard links cannot span across different filesystems.&lt;br&gt;
Syntax to create a hard link:&lt;/p&gt;

&lt;p&gt;ln  &lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 ln myfile.txt myhardlink.txt
2. Soft Link (Symbolic Link or Symlink):
— A soft link, also known as a symbolic link or symlink, is a special file that points to another file or directory by its pathname.
— Unlike a hard link, a soft link simply contains the path of the target file or directory.
— Soft links can span across different filesystems.
— Deleting the original file or directory won’t affect the soft link; it will become a “dangling” link pointing to nothing.

Syntax to create a soft link:

 ln -s &amp;lt;target_file&amp;gt; &amp;lt;soft_link_name&amp;gt;


 Example:

 ln -s /path/to/original_file.txt softlink.txt
Remember to replace `&amp;lt;original_file&amp;gt;`, `&amp;lt;hard_link_name&amp;gt;`, `&amp;lt;target_file&amp;gt;`, and `&amp;lt;soft_link_name&amp;gt;` with the appropriate file names and paths in the commands.

20. Shell scripting disadvantages
While shell scripting is powerful and widely used for automating tasks in Unix-like operating systems, it also comes with its own set of disadvantages:

1. Portability: Shell scripts are typically written for specific Unix-like operating systems such as Linux or macOS. Porting shell scripts to other platforms, such as Windows, can be challenging due to differences in shell syntax and commands.

2. Performance: Shell scripts are interpreted rather than compiled, which can lead to slower execution compared to compiled languages like C or Java, especially for complex tasks or large data processing.

3. Error Handling: Error handling in shell scripts can be more challenging compared to compiled languages. Shell scripts often rely on return codes or exit statuses of commands, which may not provide detailed information about the cause of errors.

4. Limited Functionality: While shell scripting is suitable for many system administration tasks and simple automation, it may lack the robustness and advanced features available in higher-level programming languages.

5. Security Risks: Writing secure shell scripts requires careful consideration of potential vulnerabilities, such as command injection and improper handling of user input. Insecure shell scripts can pose significant security risks to systems and data.

6. Debugging: Debugging shell scripts can be more difficult compared to compiled languages. Shell scripts may produce cryptic error messages, and debugging tools are often limited, requiring manual inspection of code and output.

7. Complexity: As shell scripts grow in size and complexity, they can become difficult to maintain and understand, especially for developers unfamiliar with shell scripting conventions and best practices.

8. Limited Support for Data Structures: Shell scripting languages like Bash have limited support for complex data structures such as arrays and associative arrays, which can make certain programming tasks more challenging.

Despite these disadvantages, shell scripting remains a valuable tool for system administration, automation, and quick prototyping of tasks in Unix-like environments. It’s essential to understand the limitations and choose the appropriate tool for the task at hand.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>bash</category>
      <category>linux</category>
      <category>devops</category>
    </item>
    <item>
      <title>Deploying a Scalable Web Application with Terraform on AWS</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Sat, 17 Aug 2024 12:09:36 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/deploying-a-scalable-web-application-with-terraform-on-aws-5e9n</link>
      <guid>https://dev.to/pratik_nalawade/deploying-a-scalable-web-application-with-terraform-on-aws-5e9n</guid>
      <description>&lt;p&gt;In today's cloud-centric world, infrastructure management has shifted from manual configurations to automation with Infrastructure as Code (IaC). Terraform, developed by HashiCorp, is a powerful IaC tool that allows us to define and provision data center infrastructure using a declarative configuration language. In this post, I'll walk you through a project where I used Terraform to deploy a scalable web application on AWS, leveraging a range of services including VPCs, EC2 instances, S3 buckets, and an Application Load Balancer (ALB).&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Project Overview&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The goal of this project is to set up a simple yet scalable web application hosted on AWS. The infrastructure includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Virtual Private Cloud (VPC) with custom subnets.&lt;/li&gt;
&lt;li&gt;An Internet Gateway and Route Table for connectivity.&lt;/li&gt;
&lt;li&gt;Security Groups to manage access.&lt;/li&gt;
&lt;li&gt;EC2 instances running Apache servers.&lt;/li&gt;
&lt;li&gt;An S3 bucket for storage.&lt;/li&gt;
&lt;li&gt;An Application Load Balancer to distribute traffic across the instances.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flu96rmo69vtyo00gt48s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flu96rmo69vtyo00gt48s.png" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;1. Setting Up Terraform and AWS Provider&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To start with Terraform, we need to configure the provider, which in this case is AWS. The &lt;code&gt;provider.tf&lt;/code&gt; file specifies the AWS provider and the region where the infrastructure will be deployed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="nx"&gt;terraform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;required_providers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;aws&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;source&lt;/span&gt;  &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"hashicorp/aws"&lt;/span&gt;
      &lt;span class="nx"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"5.11.0"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;provider&lt;/span&gt; &lt;span class="s2"&gt;"aws"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;region&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"us-east-1"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration ensures that Terraform uses the correct provider and region. It's crucial to keep the provider version updated to take advantage of the latest features and security patches.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;2. VPC and Subnet Configuration&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Next, we define the Virtual Private Cloud (VPC) where all our resources will reside. The VPC is isolated and helps secure our instances and other resources.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_vpc"&lt;/span&gt; &lt;span class="s2"&gt;"myvpc"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;cidr_block&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cidr&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also create two subnets in different availability zones to ensure high availability. These subnets will host our EC2 instances.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hcl
resource "aws_subnet" "sub1" {
  vpc_id = aws_vpc.myvpc.id
  cidr_block = "10.0.0.0/24"
  availability_zone = "us-east-1a"
  map_public_ip_on_launch = true
}

resource "aws_subnet" "sub2" {
  vpc_id = aws_vpc.myvpc.id
  cidr_block = "10.0.1.0/24"
  availability_zone = "us-east-1b"
  map_public_ip_on_launch = true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These subnets are public, allowing us to access the instances directly over the internet.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;3. Internet Gateway and Route Table&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To connect our VPC to the internet, we need an Internet Gateway. The gateway is associated with the VPC, and a route table is configured to direct internet-bound traffic through the gateway.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_internet_gateway"&lt;/span&gt; &lt;span class="s2"&gt;"igw"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;vpc_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_vpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myvpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_route_table"&lt;/span&gt; &lt;span class="s2"&gt;"RT"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;vpc_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_vpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myvpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;

  &lt;span class="nx"&gt;route&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;cidr_block&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"0.0.0.0/0"&lt;/span&gt;
    &lt;span class="nx"&gt;gateway_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_internet_gateway&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;igw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_route_table_association"&lt;/span&gt; &lt;span class="s2"&gt;"rta1"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;subnet_id&lt;/span&gt;      &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_subnet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sub1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
  &lt;span class="nx"&gt;route_table_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_route_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;RT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_route_table_association"&lt;/span&gt; &lt;span class="s2"&gt;"rta2"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;subnet_id&lt;/span&gt;      &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_subnet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sub2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
  &lt;span class="nx"&gt;route_table_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_route_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;RT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This setup ensures that any traffic from our instances can reach the internet, and vice versa.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;4. Security Groups&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Security groups act as virtual firewalls, controlling inbound and outbound traffic to our instances. In this project, I created a security group that allows HTTP (port 80) and SSH (port 22) traffic from any IP address.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_security_group"&lt;/span&gt; &lt;span class="s2"&gt;"webSg"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name_prefix&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"web-sg"&lt;/span&gt;
  &lt;span class="nx"&gt;vpc_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_vpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myvpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;

  &lt;span class="nx"&gt;ingress&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"HTTP from VPC"&lt;/span&gt;
    &lt;span class="nx"&gt;from_port&lt;/span&gt;   &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;
    &lt;span class="nx"&gt;to_port&lt;/span&gt;     &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;
    &lt;span class="nx"&gt;protocol&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"tcp"&lt;/span&gt;
    &lt;span class="nx"&gt;cidr_blocks&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"0.0.0.0/0"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;ingress&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"SSH"&lt;/span&gt;
    &lt;span class="nx"&gt;from_port&lt;/span&gt;   &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;
    &lt;span class="nx"&gt;to_port&lt;/span&gt;     &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;
    &lt;span class="nx"&gt;protocol&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"tcp"&lt;/span&gt;
    &lt;span class="nx"&gt;cidr_blocks&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"0.0.0.0/0"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;egress&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;from_port&lt;/span&gt;   &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="nx"&gt;to_port&lt;/span&gt;     &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="nx"&gt;protocol&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"-1"&lt;/span&gt;
    &lt;span class="nx"&gt;cidr_blocks&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"0.0.0.0/0"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;tags&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Web-sg"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While allowing SSH access from anywhere isn't a best practice, it's done here for simplicity. In a production environment, restrict SSH access to specific IP addresses or use a bastion host.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;5. Deploying EC2 Instances&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Two EC2 instances are deployed in different subnets, ensuring redundancy. Each instance runs an Apache server with a custom user data script that installs Apache, creates a simple HTML file, and starts the service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_instance"&lt;/span&gt; &lt;span class="s2"&gt;"webserver1"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;ami&lt;/span&gt;                    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ami-0261755bbcb8c4a84"&lt;/span&gt;
  &lt;span class="nx"&gt;instance_type&lt;/span&gt;          &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"t2.micro"&lt;/span&gt;
  &lt;span class="nx"&gt;vpc_security_group_ids&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;aws_security_group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webSg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="nx"&gt;subnet_id&lt;/span&gt;              &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_subnet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sub1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
  &lt;span class="nx"&gt;user_data&lt;/span&gt;              &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;base64encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"userdata.sh"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_instance"&lt;/span&gt; &lt;span class="s2"&gt;"webserver2"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;ami&lt;/span&gt;                    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ami-0261755bbcb8c4a84"&lt;/span&gt;
  &lt;span class="nx"&gt;instance_type&lt;/span&gt;          &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"t2.micro"&lt;/span&gt;
  &lt;span class="nx"&gt;vpc_security_group_ids&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;aws_security_group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webSg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="nx"&gt;subnet_id&lt;/span&gt;              &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_subnet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sub2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
  &lt;span class="nx"&gt;user_data&lt;/span&gt;              &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;base64encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"userdata1.sh"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The user data scripts (&lt;code&gt;userdata.sh&lt;/code&gt; and &lt;code&gt;userdata1.sh&lt;/code&gt;) are executed when the instance launches, automating the setup process. Here's an example of one of the scripts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
apt update
apt &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; apache2

&lt;span class="nv"&gt;INSTANCE_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;curl &lt;span class="nt"&gt;-s&lt;/span&gt; http://169.254.169.254/latest/meta-data/instance-id&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt; &amp;gt; /var/www/html/index.html
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;title&amp;gt;My Portfolio&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;h1&amp;gt;Terraform Project Server 1&amp;lt;/h1&amp;gt;
  &amp;lt;h2&amp;gt;Instance ID: &amp;lt;span style="color:green"&amp;gt;&lt;/span&gt;&lt;span class="nv"&gt;$INSTANCE_ID&lt;/span&gt;&lt;span class="sh"&gt;&amp;lt;/span&amp;gt;&amp;lt;/h2&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/span&gt;&lt;span class="no"&gt;EOF

&lt;/span&gt;systemctl start apache2
systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;apache2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This script updates the package lists, installs Apache, and creates a simple HTML file that displays the instance ID.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;6. Setting Up the S3 Bucket&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;An S3 bucket is also created for storage needs, which can be useful for storing logs, backups, or static assets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_s3_bucket"&lt;/span&gt; &lt;span class="s2"&gt;"example"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;bucket&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"aUnique-name-for-s3-terraform"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this project, the S3 bucket is created with a unique name and can be used for various purposes depending on the application's needs.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;7. Configuring the Application Load Balancer (ALB)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The Application Load Balancer (ALB) is set up to distribute traffic across the two EC2 instances. This ensures that the application remains available even if one instance fails.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_lb"&lt;/span&gt; &lt;span class="s2"&gt;"myalb"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;               &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"myalb"&lt;/span&gt;
  &lt;span class="nx"&gt;internal&lt;/span&gt;           &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
  &lt;span class="nx"&gt;load_balancer_type&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"application"&lt;/span&gt;
  &lt;span class="nx"&gt;security_groups&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;aws_security_group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webSg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="nx"&gt;subnets&lt;/span&gt;         &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;aws_subnet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sub1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;aws_subnet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sub2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

  &lt;span class="nx"&gt;tags&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"web"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_lb_target_group"&lt;/span&gt; &lt;span class="s2"&gt;"tg"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;     &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"myTG"&lt;/span&gt;
  &lt;span class="nx"&gt;port&lt;/span&gt;     &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;
  &lt;span class="nx"&gt;protocol&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"HTTP"&lt;/span&gt;
  &lt;span class="nx"&gt;vpc_id&lt;/span&gt;   &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_vpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myvpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;

  &lt;span class="nx"&gt;health_check&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"/"&lt;/span&gt;
    &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"traffic-port"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_lb_target_group_attachment"&lt;/span&gt; &lt;span class="s2"&gt;"attach1"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;target_group_arn&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_lb_target_group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arn&lt;/span&gt;
  &lt;span class="nx"&gt;target_id&lt;/span&gt;        &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webserver1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
  &lt;span class="nx"&gt;port&lt;/span&gt;             &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_lb_target_group_attachment"&lt;/span&gt; &lt;span class="s2"&gt;"attach2"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;target_group_arn&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_lb_target_group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arn&lt;/span&gt;
  &lt;span class="nx"&gt;target_id&lt;/span&gt;        &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webserver2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
  &lt;span class="nx"&gt;port&lt;/span&gt;             &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;resource&lt;/span&gt; &lt;span class="s2"&gt;"aws_lb_listener"&lt;/span&gt; &lt;span class="s2"&gt;"listener"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;load_balancer_arn&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_lb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myalb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arn&lt;/span&gt;
  &lt;span class="nx"&gt;port&lt;/span&gt;              &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;
  &lt;span class="nx"&gt;protocol&lt;/span&gt;          &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"HTTP"&lt;/span&gt;

  &lt;span class="nx"&gt;default_action&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;target_group_arn&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_lb_target_group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;arn&lt;/span&gt;
    &lt;span class="nx"&gt;type&lt;/span&gt;             &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"forward"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ALB is configured to listen on port 80 and forward traffic to the target group, which contains the two EC2 instances. By distributing the load between the instances, the ALB ensures higher availability and better fault tolerance.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Monitoring and Scaling
Monitoring the health of our infrastructure is crucial for maintaining the reliability and performance of the application. In this project, AWS CloudWatch is used for monitoring metrics like CPU utilization, network traffic, and instance health. By setting up CloudWatch Alarms, we can trigger actions such as auto-scaling when specific thresholds are reached.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, if the CPU utilization of an EC2 instance exceeds a certain percentage, an auto-scaling policy can be triggered to launch additional instances. This ensures that the application can handle increased traffic without degradation in performance.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Automating with Terraform Modules
As projects grow, so does the complexity of managing infrastructure. Terraform modules allow us to break down our configurations into reusable components. In this project, I created modules for the VPC, EC2 instances, and ALB, making the infrastructure more modular and easier to manage.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's an example of how the VPC module might look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hcl
Copy code
module "vpc" {
  source = "./modules/vpc"

  cidr = var.cidr
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using modules, we can easily replicate environments (e.g., staging, production) with consistent configurations, reducing the risk of errors.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
This Terraform project demonstrates how to automate the deployment of a scalable web application on AWS. By leveraging Terraform's capabilities, we can quickly spin up infrastructure that is consistent, reliable, and easily scalable. The use of modules, monitoring tools like CloudWatch, and automated scaling policies further enhances the robustness of the infrastructure.&lt;/p&gt;

&lt;p&gt;Whether you're building a simple web application or a complex multi-tier architecture, Terraform offers the flexibility and power to manage your infrastructure as code. As cloud environments continue to evolve, tools like Terraform will remain essential for managing infrastructure efficiently.&lt;/p&gt;

&lt;p&gt;This project has been a valuable learning experience, reinforcing the importance of automation, scalability, and monitoring in cloud environments. I encourage anyone interested in cloud infrastructure to explore Terraform and experiment with deploying their own projects on AWS.&lt;/p&gt;

&lt;p&gt;If you have any questions or suggestions, feel free to reach out or leave a comment. Happy coding!&lt;/p&gt;

&lt;p&gt;git repo:&lt;a href="https://github.com/PRATIKNALAWADE/Terraform-AWS/" rel="noopener noreferrer"&gt;https://github.com/PRATIKNALAWADE/Terraform-AWS/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>terraform</category>
      <category>aws</category>
    </item>
    <item>
      <title>Building and Dockerizing a MERN Stack Application</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Thu, 08 Aug 2024 14:23:54 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/building-and-dockerizing-a-mern-stack-application-1m1a</link>
      <guid>https://dev.to/pratik_nalawade/building-and-dockerizing-a-mern-stack-application-1m1a</guid>
      <description>&lt;p&gt;The MERN stack (MongoDB, Express.js, React, Node.js) is a powerful technology stack for building modern web applications. In this blog post, we’ll walk through the process of creating a simple Todo application using the MERN stack and then dockerizing it for easy deployment and scalability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnddsy9sxmsb73pg7pte2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnddsy9sxmsb73pg7pte2.png" alt="Image description" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A Closer Look at MERN Stack Components&lt;br&gt;
&lt;strong&gt;MongoDB&lt;/strong&gt;: A cross-platform document database&lt;br&gt;
MongoDB is a NoSQL (non-relational) document-oriented database. Data is stored in flexible documents with a JSON (JavaScript Object Notation)-based query language. MongoDB is known for being flexible and easy to scale.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Express&lt;/strong&gt;: A back-end web application framework&lt;br&gt;
Express is a web application framework for Node.js, another MERN component. Instead of writing full web server code by hand on Node.js directly, developers use Express to simplify the task of writing server code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt;: A JavaScript library for building user interfaces&lt;br&gt;
React was originally created by a software engineer at Facebook, and was later open-sourced. The React library can be used for creating views rendered in HTML. In a way, it’s the defining feature of the stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node.js:&lt;/strong&gt; A cross-platform JavaScript runtime environment&lt;br&gt;
Node.js is constructed on Chrome’s V8 JavaScript engine. It’s designed to build scalable network applications and can execute JavaScript code outside of a browser.&lt;/p&gt;

&lt;p&gt;Prerequisites&lt;br&gt;
Before we start, ensure you have the following installed on your machine:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node.js and npm&lt;br&gt;
MongoDB&lt;br&gt;
Docker&lt;/strong&gt;&lt;br&gt;
Step 1: Setting Up the Project&lt;br&gt;
First, create a directory for your project and initialize it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir merntodo
cd merntodo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Creating the Backend with Node.js and Express&lt;br&gt;
Initialize the backend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir server
cd server
npm init -y
npm install express mongoose cors body-parser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a basic Express server (server/server.js):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
const PORT = process.env.PORT || 5000;
app.use(cors());
app.use(bodyParser.json());
const dbURI = process.env.MONGO_URI || 'mongodb://localhost:27017/mern-todo';
mongoose.connect(dbURI, {
  useNewUrlParser: true,
  useUnifiedTopology: true
}).then(() =&amp;gt; console.log('MongoDB connected'))
  .catch(err =&amp;gt; console.log(err));
const todoRoutes = require('./routes/todo');
app.use('/api', todoRoutes);
app.listen(PORT, () =&amp;gt; {
  console.log(`Server running on port ${PORT}`);
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;use your db URL in const dbURI = process.env.MONGO_URI ||‘mongodb://localhost:27017/mern-todo’;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Define the Todo model (server/models/Todo.js):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const todoSchema = new mongoose.Schema({
  text: {
    type: String,
    required: true
  },
  completed: {
    type: Boolean,
    default: false
  }
});
module.exports = mongoose.model('Todo', todoSchema);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create routes for the Todo API (server/routes/todo.js):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const router = express.Router();
const Todo = require('../models/Todo');
// Get all todos
router.get('/todos', async (req, res) =&amp;gt; {
  try {
    const todos = await Todo.find();
    res.json(todos);
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
});

// Create a new todo
router.post('/todos', async (req, res) =&amp;gt; {
  const todo = new Todo({
    text: req.body.text
  });
  try {
    const newTodo = await todo.save();
    res.status(201).json(newTodo);
  } catch (err) {
    res.status(400).json({ message: err.message });
  }
});
// Delete a todo
router.delete('/todos/:id', async (req, res) =&amp;gt; {
  try {
    const todo = await Todo.findById(req.params.id);
    if (!todo) {
      return res.status(404).json({ message: 'Todo not found' });
    }
    await todo.remove();
    res.json({ message: 'Todo deleted' });
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
});
module.exports = router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3: Creating the Frontend with React&lt;br&gt;
Initialize the frontend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app client
cd client

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a Todo List component (client/src/TodoList.js):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useEffect } from 'react';
import axios from 'axios';
const TodoList = () =&amp;gt; {
  const [todos, setTodos] = useState([]);
  const [newTodo, setNewTodo] = useState('');
  useEffect(() =&amp;gt; {
    fetchTodos();
  }, []);
  const fetchTodos = async () =&amp;gt; {
    try {
      const res = await axios.get('http://localhost:5000/api/todos');
      setTodos(res.data);
    } catch (err) {
      console.error(err);
    }
  };
  const createTodo = async () =&amp;gt; {
    try {
      const res = await axios.post('http://localhost:5000/api/todos', { text: newTodo });
      setTodos([...todos, res.data]);
      setNewTodo('');
    } catch (err) {
      console.error(err);
    }
  };
  const deleteTodo = async (id) =&amp;gt; {
    try {
      await axios.delete(`http://localhost:5000/api/todos/${id}`);
      setTodos(todos.filter(todo =&amp;gt; todo._id !== id));
    } catch (err) {
      console.error(err);
    }
  };
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Todo List&amp;lt;/h1&amp;gt;
      &amp;lt;input 
        type="text" 
        value={newTodo} 
        onChange={(e) =&amp;gt; setNewTodo(e.target.value)} 
      /&amp;gt;
      &amp;lt;button onClick={createTodo}&amp;gt;Add Todo&amp;lt;/button&amp;gt;
      &amp;lt;ul&amp;gt;
        {todos.map(todo =&amp;gt; (
          &amp;lt;li key={todo._id}&amp;gt;
            {todo.text}
            &amp;lt;button onClick={() =&amp;gt; deleteTodo(todo._id)}&amp;gt;Delete&amp;lt;/button&amp;gt;
          &amp;lt;/li&amp;gt;
        ))}
      &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
export default TodoList;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Update client/src/App.js to include the Todo List component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import TodoList from './TodoList';
function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;TodoList /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 4: Dockerizing the Application&lt;br&gt;
Create Dockerfiles&lt;br&gt;
&lt;strong&gt;Backend Dockerfile (server/Dockerfile):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use the official Node.js image as the base image
FROM node:14
# Create and set the working directory
WORKDIR /usr/src/app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the port the app runs on
EXPOSE 5000
# Start the application
CMD ["node", "server.js"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Frontend Dockerfile (client/Dockerfile):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use the official Node.js image as the base image
FROM node:14
# Create and set the working directory
WORKDIR /usr/src/app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Build the React app
RUN npm run build
# Install serve to serve the production build
RUN npm install -g serve
# Expose the port the app runs on
EXPOSE 3000
# Start the application
CMD ["serve", "-s", "build"]
Create Docker Compose File (docker-compose.yml)
version: '3.8'
services:
  backend:
    build:
      context: ./server
      dockerfile: Dockerfile
    ports:
      - "5000:5000"
    environment:
      - NODE_ENV=development
      - MONGO_URI=mongodb://mongo:27017/mern-todo
    depends_on:
      - mongo
  frontend:
    build:
      context: ./client
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    depends_on:
      - backend
  mongo:
    image: mongo:4.2
    ports:
      - "27017:27017"
    volumes:
      - mongo-data:/data/db
volumes:
  mongo-data:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 5: Build and Run Docker Containers&lt;br&gt;
From the root directory of your project, run the following commands to build and start your Docker containers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose build
docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 6: Access Your Application&lt;br&gt;
Frontend: Open your browser and navigate to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;&lt;br&gt;
Backend: The backend API will be running at &lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;&lt;br&gt;
Conclusion&lt;br&gt;
By following these steps, you have successfully built a MERN stack Todo application and dockerized it for easy deployment. Dockerizing your application ensures that it runs consistently across different environments and simplifies the deployment process. Happy coding!&lt;/p&gt;

&lt;p&gt;code repo:github.com/PRATIKNALAWADE/Docker-mern&lt;/p&gt;

</description>
      <category>docker</category>
      <category>node</category>
      <category>devops</category>
      <category>react</category>
    </item>
    <item>
      <title>How to Setup Passwordless SSH on Linux</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Mon, 05 Aug 2024 12:23:40 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/how-to-setup-passwordless-ssh-on-linux-1bd1</link>
      <guid>https://dev.to/pratik_nalawade/how-to-setup-passwordless-ssh-on-linux-1bd1</guid>
      <description>&lt;p&gt;&lt;strong&gt;Linux fundamentals&lt;/strong&gt;: How to Setup Passwordless SSH on Linux&lt;br&gt;
In the realm of server management, secure access is paramount. SSH, or Secure Shell, stands as the cornerstone protocol for remote server administration, offering a robust and encrypted communication channel. However, traditional password-based authentication methods present challenges in terms of security and convenience.&lt;/p&gt;

&lt;p&gt;Enter passwordless SSH — a paradigm shift in authentication that streamlines access while fortifying security. In this comprehensive guide, we’ll explore the intricacies of setting up passwordless SSH on Linux-based systems like Ubuntu and CentOS, empowering you to elevate your server management practices.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnkfujde3v6pj3trdj3um.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnkfujde3v6pj3trdj3um.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages of Passwordless SSH&lt;/strong&gt;:&lt;br&gt;
Before delving into implementation, let’s examine why passwordless SSH is gaining traction among sysadmins and DevOps professionals alike. Passwordless SSH, also known as public key-based authentication, offers several compelling advantages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enhanced Security&lt;/strong&gt;: By leveraging public-private key cryptography, passwordless SSH eliminates the vulnerabilities associated with password-based authentication, fortifying your server against brute-force attacks and unauthorized access attempts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Streamlined Access&lt;/strong&gt;: With passwordless SSH, users enjoy seamless and non-interactive login experiences, eliminating the need to repeatedly enter passwords for each session. This enhances productivity and simplifies remote server management tasks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Robust Authentication&lt;/strong&gt;: Public key-based authentication provides a more robust and reliable means of verifying user identities, fostering a foundation for stringent authentication and authorization policies.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let’s embark on the journey of setting up passwordless SSH on your Linux server, exploring three distinct methods for implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 1: Leveraging ssh-copy-id for Effortless Key Distribution&lt;/strong&gt;&lt;br&gt;
The ssh-copy-id command simplifies the process of distributing your public key to remote servers, automating the appending of the key to the authorized_keys file. Follow these steps to utilize ssh-copy-id:&lt;/p&gt;

&lt;p&gt;Step 1: Generate a Public-Private Key Pair: Use the ssh-keygen command to generate a key pair on your local machine.&lt;br&gt;
Step 2: Copy the Public Key: Execute ssh-copy-id remote_username@remote_IP_Address, and authenticate with the remote server’s password when prompted.&lt;br&gt;
Step 3: Verify Connectivity: Attempt to SSH into the remote server — if successful, you’ve configured passwordless SSH using ssh-copy-id.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 2: SSH-Based Key Distribution for Flexibility and Control&lt;/strong&gt;&lt;br&gt;
For scenarios where ssh-copy-id isn’t available, SSH-based key distribution offers a viable alternative. Follow these steps to distribute your public key using SSH:&lt;/p&gt;

&lt;p&gt;Step 1: Generate a Public-Private Key Pair: Utilize ssh-keygen to generate a key pair on your local machine.&lt;br&gt;
Step 2: Copy the Public Key: Execute the command cat ~/.ssh/id_rsa.pub | ssh remote_username@remote_ip_address “mkdir -p ~/.ssh &amp;amp;&amp;amp; cat &amp;gt;&amp;gt; ~/.ssh/authorized_keys”.&lt;br&gt;
Step 3: Validate Configuration: Attempt to SSH into the remote server — successful login indicates successful passwordless SSH setup using SSH-based key distribution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 3: Manual Key Insertion for Unyielding Situations&lt;/strong&gt;&lt;br&gt;
In scenarios where automated methods fail, manual key insertion provides a fail-safe approach. Follow these steps to manually insert your public key:&lt;/p&gt;

&lt;p&gt;Step 1: Generate a Public-Private Key Pair: Create a key pair using ssh-keygen on your local machine.&lt;br&gt;
Step 2: Copy the Public Key: Display the contents of the id_rsa.pub file using cat ~/.ssh/id_rsa.pub, and manually append the key to the remote server’s authorized_keys file.&lt;br&gt;
Step 3: Secure Permissions: Ensure proper permissions are set for the .ssh directory and authorized_keys file using chmod, and validate the configuration by attempting to SSH into the remote server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;br&gt;
In conclusion, passwordless SSH represents a paradigm shift in secure server access, offering enhanced security, streamlined access, and robust authentication mechanisms. By mastering the methods outlined in this guide, you can elevate your server management practices and fortify your infrastructure against potential security threats. Embrace the power of passwordless SSH and unlock a new realm of efficiency and security in your server management endeavors.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>linux</category>
      <category>ubuntu</category>
      <category>learning</category>
    </item>
    <item>
      <title>Mastering Git for DevOps: A Comprehensive Guide</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Sat, 03 Aug 2024 06:08:36 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/mastering-git-for-devops-a-comprehensive-guide-4h6p</link>
      <guid>https://dev.to/pratik_nalawade/mastering-git-for-devops-a-comprehensive-guide-4h6p</guid>
      <description>&lt;p&gt;Welcome to our tech blog focused on mastering Git for DevOps interviews. In this guide, I’ll cover everything from the basics of Git to advanced topics, ensuring you’re well-prepared to ace any Git-related questions during your DevOps interview.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Version Control Systems
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Difference between CVCS and DVCS
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;CVCS (Centralized Version Control System)&lt;/strong&gt;: In CVCS, there’s a central server that stores all versions of a project’s files. Users check out files from this central repository to work on them and then check them back in when done.&lt;br&gt;
&lt;strong&gt;DVCS (Distributed Version Control System)&lt;/strong&gt;: Unlike CVCS, DVCS does not necessarily rely on a central server. Each user has a complete copy (clone) of the repository, including its full history. This allows for offline work and more flexibility in collaboration.&lt;br&gt;
&lt;strong&gt;Importance of Git&lt;/strong&gt;&lt;br&gt;
Git has become the de facto standard for version control in modern software development due to its numerous advantages:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Distributed nature&lt;/strong&gt;: Allows for flexible and decentralized workflows.&lt;br&gt;
&lt;strong&gt;Branching and merging&lt;/strong&gt;: Facilitates parallel development and easy integration of features.&lt;br&gt;
&lt;strong&gt;Speed:&lt;/strong&gt; Git is incredibly fast, making it ideal for both small and large projects.&lt;br&gt;
&lt;strong&gt;Data integrity&lt;/strong&gt;: Uses cryptographic hashing to ensure the integrity of your data.&lt;br&gt;
&lt;strong&gt;Large community and ecosystem&lt;/strong&gt;: There’s extensive documentation, support, and a plethora of third-party tools and integrations available.&lt;/p&gt;
&lt;h3&gt;
  
  
  Git Three-stage Architecture
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Git has three main stages:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;Working Directory&lt;/strong&gt;: The directory where you do all your work.&lt;br&gt;
2.&lt;strong&gt;Staging Area (Index)&lt;/strong&gt;: Acts as a buffer between the working directory and the repository. Files in the staging area are ready to be committed.&lt;br&gt;
3.&lt;strong&gt;Repository (HEAD)&lt;/strong&gt;: The repository stores all committed changes and their history.&lt;br&gt;
Understanding Key Git Concepts&lt;br&gt;
Repository, Commit, Tags, Snapshots&lt;br&gt;
&lt;strong&gt;Repository&lt;/strong&gt;: A Git repository is a directory that contains all the files and folders of a project, along with metadata and version history.&lt;br&gt;
&lt;strong&gt;Commit&lt;/strong&gt;: A commit represents a snapshot of changes to the repository at a particular point in time.&lt;br&gt;
&lt;strong&gt;Tags:&lt;/strong&gt; Tags are pointers to specific commits, often used to mark release versions.&lt;br&gt;
&lt;strong&gt;Snapshots&lt;/strong&gt;: Git uses snapshots to store changes to files over time, rather than storing the changes themselves.&lt;br&gt;
&lt;strong&gt;Push-Pull Mechanism and Branching Strategy&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Push-Pull Mechanism&lt;/strong&gt;: Git uses the push and pull commands to synchronize changes between your local repository and a remote repository.&lt;br&gt;
push =&amp;gt; code changes uploaded into remote repository&lt;br&gt;
pull =&amp;gt; code changes downloaded into local repository&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Branching Strategy&lt;/strong&gt;: Git’s branching model allows for parallel development by creating separate branches for features, bug fixes, etc. Common strategies include GitFlow and GitHub Flow.&lt;/p&gt;
&lt;h3&gt;
  
  
  Advanced Git Operations
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Working with Git Stash and Git Pop&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Git Stash&lt;/strong&gt;: Stashing allows you to temporarily shelve changes in your working directory, allowing you to switch to a different branch or work on a different task.&lt;br&gt;
&lt;strong&gt;Git Pop&lt;/strong&gt;: Pop applies the most recently stashed changes back to your working directory.&lt;br&gt;
&lt;strong&gt;Use Case&lt;/strong&gt;: Suppose you’re working on a feature branch and need to quickly switch to another branch to address an urgent bug. However, your changes are not ready for commit. Here, Git stash comes in handy to temporarily store your changes.&lt;/p&gt;

&lt;p&gt;example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
# Stash changes
git stash

# Switch to another branch
git checkout bug-fix

# Make necessary changes and commit

# Switch back to the feature branch
git checkout feature-branch

# Apply stashed changes
git stash pop

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Resolving Merge Conflicts in Git&lt;br&gt;
Merge conflicts occur when Git is unable to automatically merge changes from different branches. To resolve conflicts, you need to manually edit the conflicting files to incorporate the desired changes.&lt;/p&gt;

&lt;p&gt;Use Case: When you merge branches and Git encounters conflicting changes, it pauses the merge process and prompts you to resolve the conflicts manually.&lt;/p&gt;

&lt;p&gt;example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Switch to the branch you want to merge into
git checkout main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Merge the feature branch into main
&lt;/h1&gt;

&lt;p&gt;git merge feature-branch&lt;br&gt;
When conflicts occur, Git will display the conflicted files. Open the conflicted files in your code editor and resolve the conflicts. After resolving conflicts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Add the resolved files
git add 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Commit the merge
&lt;/h1&gt;

&lt;p&gt;git commit -m "Merge feature-branch into main, resolved conflicts"&lt;br&gt;
Git Revert and Reset (Reset vs Revert)&lt;br&gt;
Git Revert: Revert undoes a previous commit by creating a new commit that undoes the changes introduced by the original commit.&lt;br&gt;
Git Reset: Reset moves the HEAD and current branch pointer to a specified commit, optionally modifying the working directory and staging area.&lt;br&gt;
Use Case:&lt;/p&gt;

&lt;p&gt;Git Revert: Let’s say you’ve pushed a commit containing a bug, and you want to undo the changes introduced by that commit without altering the commit history. Revert creates a new commit that undoes the changes.&lt;br&gt;
Git Reset: Suppose you’ve made local commits that you want to discard entirely, resetting the branch to a previous state. Reset moves the HEAD and branch pointers to a specified commit.&lt;br&gt;
Syntax:&lt;/p&gt;

&lt;h1&gt;
  
  
  Revert a commit
&lt;/h1&gt;

&lt;p&gt;git revert &lt;/p&gt;

&lt;h1&gt;
  
  
  Reset to a previous commit
&lt;/h1&gt;

&lt;p&gt;git reset [--soft | --mixed | --hard] &lt;br&gt;
Working with Git Squash&lt;br&gt;
Squashing commits combines multiple commits into a single commit. This is often done to clean up the commit history before merging a feature branch into the main branch.&lt;/p&gt;

&lt;p&gt;Use Case: Before merging a feature branch into the main branch, you want to clean up the commit history by combining multiple small, related commits into a single, meaningful commit.&lt;/p&gt;

&lt;p&gt;example:&lt;/p&gt;

&lt;h1&gt;
  
  
  Start an interactive rebase
&lt;/h1&gt;

&lt;p&gt;git rebase -i HEAD~3  # Squash last 3 commits&lt;/p&gt;

&lt;h1&gt;
  
  
  In the interactive rebase editor, change 'pick' to 'squash' for commits you want to squash
&lt;/h1&gt;

&lt;p&gt;What is Git Fork?&lt;br&gt;
A Git fork is a copy of a repository that allows you to freely experiment with changes without affecting the original repository. Forks are commonly used in open-source collaboration on platforms like GitHub.&lt;/p&gt;

&lt;p&gt;git rebase, git merge, and git cherry-pick:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Git Merge
Use Case:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Merge is a straightforward way to integrate changes from one branch into another.&lt;br&gt;
It creates a new commit that combines the commit histories of the merged branches, preserving the history of each branch.&lt;br&gt;
Syntax:&lt;/p&gt;

&lt;h1&gt;
  
  
  Merge feature-branch into main
&lt;/h1&gt;

&lt;p&gt;git checkout main&lt;br&gt;
git merge feature-branch&lt;br&gt;
Pros:&lt;/p&gt;

&lt;p&gt;Preserves the history of both branches.&lt;br&gt;
Easy to understand and use.&lt;br&gt;
Cons:&lt;/p&gt;

&lt;p&gt;Can result in a cluttered commit history, especially in long-running projects with many branches.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Git Rebase
Use Case:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Rebase is used to incorporate changes from one branch into another by reapplying commits on top of another branch.&lt;br&gt;
It creates a linear history, making the commit history cleaner and easier to follow.&lt;br&gt;
Syntax:&lt;/p&gt;

&lt;h1&gt;
  
  
  Rebase feature-branch onto main
&lt;/h1&gt;

&lt;p&gt;git checkout feature-branch&lt;br&gt;
git rebase main&lt;br&gt;
Pros:&lt;/p&gt;

&lt;p&gt;Creates a cleaner, linear commit history.&lt;br&gt;
Simplifies the process of integrating changes from feature branches into the main branch.&lt;br&gt;
Cons:&lt;/p&gt;

&lt;p&gt;Rewrites commit history, which can lead to conflicts and potential loss of work if not used carefully.&lt;br&gt;
Should not be used on shared branches as it alters commit history.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Git Cherry-pick
Use Case:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Cherry-pick allows you to select specific commits from one branch and apply them to another branch.&lt;br&gt;
Useful for applying individual commits, such as bug fixes or specific features, to another branch without merging the entire branch.&lt;br&gt;
Syntax:&lt;/p&gt;

&lt;h1&gt;
  
  
  Cherry-pick a commit from feature-branch into main
&lt;/h1&gt;

&lt;p&gt;git checkout main&lt;br&gt;
git cherry-pick &lt;br&gt;
Pros:&lt;/p&gt;

&lt;p&gt;Allows for selective integration of commits.&lt;br&gt;
Useful for applying critical bug fixes or specific features to release branches.&lt;br&gt;
Cons:&lt;/p&gt;

&lt;p&gt;Can lead to disjointed commit histories if used excessively.&lt;br&gt;
May introduce conflicts that need to be resolved manually.&lt;br&gt;
Choosing the Right Approach&lt;br&gt;
Merge: Use when you want to preserve the commit history of both branches and don’t mind a potentially cluttered history.&lt;br&gt;
Rebase: Ideal for creating a clean, linear commit history and integrating changes from feature branches into the main branch.&lt;br&gt;
Cherry-pick: Useful for selectively applying individual commits to another branch without merging the entire branch.&lt;br&gt;
Each approach has its strengths and weaknesses, so it’s essential to understand when to use each one based on your project’s needs and collaboration workflow.&lt;/p&gt;

&lt;p&gt;Git Integration on VScode, Git Authentication with GitHub via SSH and HTTPS Protocol&lt;br&gt;
Visual Studio Code (VS Code) has built-in Git integration, allowing you to perform most Git operations directly within the editor. You can authenticate with GitHub using SSH keys or HTTPS credentials for secure access to repositories.&lt;/p&gt;

&lt;p&gt;GitHub Integration and Collaboration&lt;br&gt;
GitHub Introduction, Creating Repositories, PR’s&lt;br&gt;
GitHub is a popular platform for hosting Git repositories and collaborating on projects. You can create repositories, open pull requests (PRs) to propose changes, review code, and merge changes into the main branch.&lt;/p&gt;

&lt;p&gt;Thus, with this comprehensive guide, you should now have a solid understanding of Git, from basic version control concepts to advanced operations and collaboration on platforms like GitHub.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Networking fundamental concepts for DevOps</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Fri, 02 Aug 2024 19:48:57 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/networking-fundamental-concepts-for-devops-1bkg</link>
      <guid>https://dev.to/pratik_nalawade/networking-fundamental-concepts-for-devops-1bkg</guid>
      <description>&lt;p&gt;Relevance of Networking to DevOps&lt;br&gt;
DevOps champions collaboration, automation, and efficient delivery of software. Networking plays a big role in achieving these objectives by enabling various aspects of a DevOps environment:&lt;/p&gt;

&lt;p&gt;Infrastructure Provisioning: Networking knowledge is essential for configuring and managing virtualized environments, such as cloud platforms or on-premises data centers. Understanding networking concepts helps in setting up and connecting different resources effectively.&lt;br&gt;
Scalability and Performance: DevOps teams strive to create scalable and high-performance systems. Networking skills enable engineers to design and optimize network architecture, load balancing, and traffic management to meet these requirements.&lt;br&gt;
Security and Monitoring: Networking forms the foundation for implementing robust security measures and monitoring solutions. Concepts like firewalls, VPNs, and network monitoring tools are essential for ensuring the safety and reliability of systems.&lt;br&gt;
Continuous Integration and Deployment: Networking expertise allows DevOps professionals to design efficient continuous integration and deployment (CI/CD) pipelines. This involves configuring network connectivity between various components involved in the delivery process.&lt;br&gt;
IPv4 addressing subnetting, and CIDR + subnet masking&lt;br&gt;
CIDR (Classless Inter-Domain Routing) is a way to efficiently manage and allocate IP addresses in an IPv4 network. It combines the concept of IP addresses with a subnet mask into a single notation, making it easier to understand how many usable IP addresses a specific network segment can hold.&lt;/p&gt;

&lt;p&gt;Here’s a breakdown of CIDR subnetting:&lt;/p&gt;

&lt;p&gt;IP Addresses:&lt;/p&gt;

&lt;p&gt;An IPv4 address consists of 32 bits, typically represented in four decimal octets separated by dots (e.g., 192.168.1.0).&lt;br&gt;
Each octet can have a value between 0 and 255.&lt;br&gt;
Subnet Mask:&lt;/p&gt;

&lt;p&gt;Traditionally, subnetting involved using a subnet mask (also represented in four octets) to divide the IP address into two parts:&lt;br&gt;
Network address: Identifies the network itself and cannot be assigned to a device.&lt;br&gt;
Host address: Identifies individual devices within the network.&lt;br&gt;
The subnet mask defines the boundary between the network and host portions using ones (1) for the network part and zeros (0) for the host part. For example, a subnet mask of 255.255.255.0 allocates the first three octets (24 bits) for the network and the last octet (8 bits) for the host addresses.&lt;br&gt;
CIDR Notation:&lt;/p&gt;

&lt;p&gt;CIDR simplifies subnet masking by expressing the number of network bits directly in the IP address notation. It uses a forward slash (/) followed by the number of bits dedicated to the network portion.&lt;br&gt;
For example, 192.168.1.0/24 is equivalent to the combination of the IP address 192.168.1.0 and a subnet mask of 255.255.255.0 (both indicate the first 24 bits define the network).&lt;br&gt;
Benefits of CIDR Subnetting:&lt;/p&gt;

&lt;p&gt;Efficient IP Address Allocation: CIDR allows for creating subnets with varying sizes depending on the number of devices needed. This avoids wasting a large pool of addresses in a small network or cramming too many devices into a limited address space.&lt;br&gt;
Hierarchical Routing: CIDR simplifies inter-domain routing by allowing routers to identify the network portion of an IP address quickly, enabling efficient routing of traffic.&lt;br&gt;
Flexibility: CIDR offers more flexibility for network administrators to design subnets that match specific needs.&lt;br&gt;
Calculating Usable IP Addresses:&lt;/p&gt;

&lt;p&gt;Check out cidr.xyz for interactive session&lt;br&gt;
With CIDR notation, you can easily calculate the number of usable IP addresses within a subnet. Here’s the formula:&lt;/p&gt;

&lt;p&gt;Usable IP addresses = 2^(number of host bits) — 2 (exclude network and broadcast addresses)&lt;br&gt;
For example, in the subnet 10.0.1.0/24 (24 network bits), there are 8 host bits (32 total bits — 24 network bits). So, the usable IP addresses would be 2⁸ — 2 = 254 (excluding the network address 10.0.1.0 and the broadcast address 10.0.1.255).&lt;/p&gt;

&lt;p&gt;Summary:&lt;/p&gt;

&lt;p&gt;CIDR subnetting is a fundamental concept in network design and management. By understanding how CIDR notation works, you can effectively allocate IP addresses within your network, ensuring efficient resource utilization and proper network communication.&lt;/p&gt;

&lt;p&gt;video explaining the same:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=aPW-ZAo09Pg" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=aPW-ZAo09Pg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OSI Layer&lt;/p&gt;

&lt;p&gt;The OSI (Open Systems Interconnection) model is a conceptual framework that defines network communication into seven distinct layers. Each layer has specific functionalities and protocols that DevOps engineers should be familiar with to troubleshoot network issues, manage infrastructure, and ensure efficient communication within their systems. Here’s a breakdown of the 7 layers and relevant protocols from a DevOps perspective:&lt;/p&gt;

&lt;p&gt;Layer 1: Physical Layer&lt;/p&gt;

&lt;p&gt;Function: Deals with the physical transmission of raw data bits across a physical medium like cables or wireless signals.&lt;br&gt;
DevOps Relevance: Understanding physical connectivity issues like faulty cables or incorrect port configurations is crucial for troubleshooting basic network connectivity problems.&lt;br&gt;
Layer 2: Data Link Layer&lt;/p&gt;

&lt;p&gt;Function: Handles error detection and correction at the frame level (groups of data bits).&lt;br&gt;
Important Protocol: Ethernet (Most common LAN protocol for error-free data transmission on wired networks)&lt;br&gt;
DevOps Relevance: Knowledge of Ethernet and troubleshooting tools like ping can help identify issues with network devices like switches and network interface cards (NICs).&lt;br&gt;
Layer 3: Network Layer&lt;/p&gt;

&lt;p&gt;Function: Responsible for routing packets (datagrams) across networks based on IP addresses.&lt;br&gt;
Important Protocol: IP (Internet Protocol) — Defines the addressing scheme (IPv4 or IPv6) for identifying devices on a network.&lt;br&gt;
DevOps Relevance: Understanding IP addressing and subnetting is essential for configuring network devices, managing cloud resources (VPCs), and troubleshooting routing issues.&lt;br&gt;
Layer 4: Transport Layer&lt;/p&gt;

&lt;p&gt;Function: Provides reliable or unreliable data transfer between applications on different devices.&lt;br&gt;
Important Protocols:&lt;br&gt;
TCP (Transmission Control Protocol): Ensures reliable, in-order delivery of data with error checking and retransmission.&lt;br&gt;
UDP (User Datagram Protocol): Provides connectionless, best-effort data delivery suitable for real-time applications like video streaming.&lt;br&gt;
DevOps Relevance: Understanding TCP and UDP is crucial for troubleshooting application communication issues and choosing the appropriate protocol for different DevOps tools and deployments.&lt;br&gt;
Layer 5: Session Layer&lt;/p&gt;

&lt;p&gt;Function: Establishes, manages, and terminates sessions between communicating applications.&lt;br&gt;
Important Protocol: SSH (Secure Shell) — Provides secure remote access and communication for DevOps engineers to manage servers and infrastructure.&lt;br&gt;
DevOps Relevance: SSH is a fundamental tool for DevOps engineers to access and manage remote systems securely.&lt;br&gt;
Layer 6: Presentation Layer&lt;/p&gt;

&lt;p&gt;Function: Deals with data format and encryption/decryption before transmission and after reception.&lt;br&gt;
Important Protocol: HTTPS (Hypertext Transfer Protocol Secure) — Encrypts communication between web servers and clients, protecting data integrity.&lt;br&gt;
DevOps Relevance: Understanding protocols like HTTPS is essential for securing application communication and ensuring data privacy.&lt;br&gt;
Layer 7: Application Layer&lt;/p&gt;

&lt;p&gt;Function: Provides network services directly to user applications.&lt;br&gt;
Important Protocols:&lt;br&gt;
HTTP (Hypertext Transfer Protocol): The foundation of web communication, used for data exchange between web browsers and servers.&lt;br&gt;
DNS (Domain Name System): Translates human-readable domain names (like [invalid URL removed]) into machine-readable IP addresses.&lt;br&gt;
FTP (File Transfer Protocol): Used for transferring files between computers on a network.&lt;br&gt;
DevOps Relevance: Familiarity with application layer protocols is essential for DevOps engineers to deploy and manage web applications, configure load balancers, and troubleshoot application communication issues.&lt;br&gt;
In summary:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqslisbvjqfyvt178h8xa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqslisbvjqfyvt178h8xa.png" alt="Image description" width="800" height="620"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The OSI model provides a framework for understanding network communication. While not a strict implementation in modern networks, each layer plays a role, and the associated protocols are crucial for DevOps engineers to manage infrastructure, troubleshoot network issues, and ensure efficient communication within their systems.&lt;/p&gt;

&lt;p&gt;TCP is a connection-oriented protocol. This means that it first establishes a link between the source and destination before it sends data. Once the connection has been made, then TCP breaks down large data sets into smaller packets, sends them along the connection, and ensures data integrity throughout the entire process. TCP is a preferred protocol when data integrity is critical, such as in any transactional system.&lt;/p&gt;

&lt;p&gt;UDP in turn is not connection-oriented. UDP starts transmitting data immediately, without waiting for connection confirmation from the receiving side. Even though some data loss can happen, UDP is most often used in cases where speed is more important than perfect transmission, such as in voice or video streaming&lt;/p&gt;

&lt;p&gt;Domain Name System (DNS)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vvmpyam2djxq57wosge.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vvmpyam2djxq57wosge.png" alt="Image description" width="800" height="691"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Client initiates a query to a Recursive Resolver.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Recursive Resolver connects to a Root Server.&lt;/li&gt;
&lt;li&gt;The Root Nameserver then responds to the resolver with the address of a Top Level Domain (TLD) Server (such as .com or .net)&lt;/li&gt;
&lt;li&gt;The Root Resolver makes a request to the TLD Server.&lt;/li&gt;
&lt;li&gt;The TLD Server returns the IP address of the Domain Nameserver, which stores the information about the requested domain.&lt;/li&gt;
&lt;li&gt;The Recursive Resolver sends a query to the Domain Nameserver.&lt;/li&gt;
&lt;li&gt;The IP address for the requested domain is returned to the Recursive Resolver from the Domain Nameserver.&lt;/li&gt;
&lt;li&gt;The Recursive Resolver provides the client with the IP address of the requested domain.
DNS record types
DNS records, also known as zone files, provide information about a domain. This includes the IP address that is associated with this domain and how to handle queries for it. Each DNS record has a time-to-live setting (TTL) which indicates how often a DNS server will refresh it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Below are the most commonly used types of DNS records and their meaning.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydu2sa97a6q5009rw9a3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fydu2sa97a6q5009rw9a3.png" alt="Image description" width="800" height="529"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HTTP&lt;br&gt;
HTTP Methods&lt;br&gt;
You’ve probably heard about Hypertext Transfer Protocol, also known as HTTP. HTTP allows you to interact with Web pages, HTML documents, and APIs. It is the foundation of any data exchange on the Internet&lt;/p&gt;

&lt;p&gt;There are 7 main HTTP request methods:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbbfivel8050void3o5q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbbfivel8050void3o5q.png" alt="Image description" width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are 4 categories of HTTP responses:&lt;/p&gt;

&lt;p&gt;200s: Successful responses&lt;br&gt;
300s: Redirects&lt;br&gt;
400s: Client errors&lt;br&gt;
500s: Server errors&lt;br&gt;
HTTP Headers&lt;br&gt;
HTTP headers allow the client to add additional information to a request for purposes such as authentication, caching, and specifying the type of client device sending the request.&lt;br&gt;
Headers fall into 4 general contexts:&lt;/p&gt;

&lt;p&gt;General Header: A header that works for both response and requests messages.&lt;br&gt;
Request Header: A header that only applies to request messages from a client.&lt;br&gt;
Response Header: A header that only applies to responses from a server.&lt;br&gt;
Entity Header: A header that gives information about the entity itself or the resource requested.&lt;br&gt;
Network Troubleshooting Tools&lt;br&gt;
DevOps engineers rely on various tools to diagnose and troubleshoot network connectivity issues, ensuring optimal performance and reliability within their systems. Here are some essential network troubleshooting tools commonly used by DevOps professionals:&lt;/p&gt;

&lt;p&gt;Basic Command-Line Tools:&lt;/p&gt;

&lt;p&gt;ping: The most fundamental tool used to test basic network connectivity between devices by sending echo requests and waiting for responses. It helps identify if a specific device is reachable on the network.&lt;br&gt;
traceroute/tracert: Reveals the path that data packets take to reach a destination. This helps pinpoint where potential network delays or outages might be occurring along the route.&lt;br&gt;
netstat: Provides information about network connections, routing tables, and network interface statistics. Useful for identifying active connections, listening ports, and potential bottlenecks.&lt;br&gt;
nslookup: Looks up information about domain names, including translating them into corresponding IP addresses. This helps verify DNS resolution functionality.&lt;br&gt;
Advanced Command-Line Tools:&lt;/p&gt;

&lt;p&gt;nmap: A powerful network scanner that can identify devices on a network, operating systems, open ports, and services running on those ports. This aids in comprehensive network security assessments and vulnerability discovery.&lt;br&gt;
tcpdump/Wireshark: Packet capture and analysis tools. They capture network traffic on a specific interface, allowing you to inspect individual data packets and analyze their contents to diagnose communication issues or identify security threats.&lt;br&gt;
curl: A command-line tool for transferring data from or to servers. It can be used to test web server functionality and diagnose HTTP communication problems.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>learning</category>
      <category>beginners</category>
      <category>coding</category>
    </item>
    <item>
      <title>Python for devops</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Wed, 31 Jul 2024 14:27:37 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/python-for-devops-15hg</link>
      <guid>https://dev.to/pratik_nalawade/python-for-devops-15hg</guid>
      <description>&lt;p&gt;Here are some important Python modules used for DevOps automation:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;os module&lt;/strong&gt;: The os module provides a way to interact with the operating system, including file operations, process management, and system information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requests and urllib3 modules&lt;/strong&gt;: The Requests and urllib3 modules are used to send HTTP requests and handle HTTP responses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Logging module&lt;/strong&gt;: The logging module provides a way to log messages from Python applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;boto3 module&lt;/strong&gt;: The boto3 module provides an interface to the Amazon Web Services (AWS) SDK for Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;paramiko module&lt;/strong&gt; : The paramiko module is a Python implementation of SSH protocol, used for secure remote connections.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSON module&lt;/strong&gt; : The JSON module is used to encode and decode JSON data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PyYAML module&lt;/strong&gt; : The PyYAML module provides a way to parse and generate YAML data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pandas module&lt;/strong&gt;: The pandas module provides data analysis tools, including data manipulation and data visualization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;smtplib module&lt;/strong&gt;: The smtplib module provides a way to send email messages from Python applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Python Use Cases in DevOps
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1.&lt;strong&gt;Automation of Infrastructure Provisioning&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tooling:&lt;/strong&gt; AWS Boto3, Azure SDK, Terraform, Ansible&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt; Automating the creation and management of cloud resources such as EC2 instances, S3 buckets, and RDS databases. Python scripts can use the AWS Boto3 library to manage AWS resources programmatically.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;example code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import boto3

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')

    # Get all EBS snapshots
    response = ec2.describe_snapshots(OwnerIds=['self'])

    # Get all active EC2 instance IDs
    instances_response = ec2.describe_instances(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
    active_instance_ids = set()

    for reservation in instances_response['Reservations']:
        for instance in reservation['Instances']:
            active_instance_ids.add(instance['InstanceId'])

    # Iterate through each snapshot and delete if it's not attached to any volume or the volume is not attached to a running instance
    for snapshot in response['Snapshots']:
        snapshot_id = snapshot['SnapshotId']
        volume_id = snapshot.get('VolumeId')

        if not volume_id:
            # Delete the snapshot if it's not attached to any volume
            ec2.delete_snapshot(SnapshotId=snapshot_id)
            print(f"Deleted EBS snapshot {snapshot_id} as it was not attached to any volume.")
        else:
            # Check if the volume still exists
            try:
                volume_response = ec2.describe_volumes(VolumeIds=[volume_id])
                if not volume_response['Volumes'][0]['Attachments']:
                    ec2.delete_snapshot(SnapshotId=snapshot_id)
                    print(f"Deleted EBS snapshot {snapshot_id} as it was taken from a volume not attached to any running instance.")
            except ec2.exceptions.ClientError as e:
                if e.response['Error']['Code'] == 'InvalidVolume.NotFound':
                    # The volume associated with the snapshot is not found (it might have been deleted)
                    ec2.delete_snapshot(SnapshotId=snapshot_id)
                    print(f"Deleted EBS snapshot {snapshot_id} as its associated volume was not found.")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lambda function is designed to clean up unused EBS snapshots. Here's a brief rundown of what it does:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Describe Snapshots&lt;/strong&gt;: Retrieves all EBS snapshots owned by the account.&lt;br&gt;
&lt;strong&gt;Describe Instances&lt;/strong&gt;: Retrieves all running EC2 instances.&lt;br&gt;
Iterate through Snapshots: For each snapshot, checks if it's attached to a volume.&lt;br&gt;
If the snapshot is not attached to any volume, it deletes the snapshot.&lt;br&gt;
If the snapshot is attached to a volume, it checks if the volume is attached to any running instance.&lt;br&gt;
If the volume is not attached to any running instance or the volume does not exist, it deletes the snapshot.&lt;br&gt;
repo:&lt;a href="https://github.com/PRATIKNALAWADE/AWS-Cost-Optimization/blob/main/ebs_snapshots.py" rel="noopener noreferrer"&gt;https://github.com/PRATIKNALAWADE/AWS-Cost-Optimization/blob/main/ebs_snapshots.py&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  2.&lt;strong&gt;Use Case: Automating CI/CD Pipelines with Python&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In a CI/CD pipeline, automation is key to ensuring that code changes are built, tested, and deployed consistently and reliably. Python can be used to interact with CI/CD tools like Jenkins, GitLab CI, or CircleCI, either by triggering jobs, handling webhook events, or interacting with various APIs to deploy applications.&lt;/p&gt;

&lt;p&gt;Below is an example of how you can use Python to automate certain aspects of a CI/CD pipeline using Jenkins.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Triggering Jenkins Jobs with Python&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;br&gt;
You have a Python script that needs to trigger a Jenkins job whenever a new commit is pushed to the &lt;code&gt;main&lt;/code&gt; branch of a GitHub repository. The script will also pass some parameters to the Jenkins job, such as the Git commit ID and the branch name.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Step 1: Set Up Jenkins Job&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;First, ensure that you have a Jenkins job configured to accept parameters. You will need the job name, Jenkins URL, and an API token for authentication.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Step 2: Writing the Python Script&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Below is a Python script that triggers the Jenkins job with specific parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="c1"&gt;# Jenkins server details
&lt;/span&gt;&lt;span class="n"&gt;jenkins_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;http://your-jenkins-server.com&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;job_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;your-job-name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;your-username&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;api_token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;your-api-token&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;# Parameters to pass to the Jenkins job
&lt;/span&gt;&lt;span class="n"&gt;branch_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;main&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;commit_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;abc1234def5678&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;# Construct the job URL
&lt;/span&gt;&lt;span class="n"&gt;job_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;jenkins_url&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/job/&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;job_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/buildWithParameters&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;# Define the parameters to pass
&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;BRANCH_NAME&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;branch_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;COMMIT_ID&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;commit_id&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Trigger the Jenkins job
&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;job_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;api_token&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Check the response
&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Jenkins job triggered successfully.&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Failed to trigger Jenkins job: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Step 3: Explanation&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Jenkins Details:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;jenkins_url&lt;/code&gt;: URL of your Jenkins server.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;job_name&lt;/code&gt;: The name of the Jenkins job you want to trigger.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;username&lt;/code&gt; and &lt;code&gt;api_token&lt;/code&gt;: Your Jenkins credentials for authentication.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Parameters:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;branch_name&lt;/code&gt; and &lt;code&gt;commit_id&lt;/code&gt; are examples of parameters that the Jenkins job will use. These could be passed dynamically based on your CI/CD workflow.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Requests Library:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The script uses Python's &lt;code&gt;requests&lt;/code&gt; library to make a POST request to the Jenkins server to trigger the job.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;auth=(username, api_token)&lt;/code&gt; is used to authenticate with the Jenkins API.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Response Handling:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the job is triggered successfully, Jenkins responds with a &lt;code&gt;201&lt;/code&gt; status code, which the script checks to confirm success.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 4: Integrate with GitHub Webhooks&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;To trigger this Python script automatically whenever a new commit is pushed to the &lt;code&gt;main&lt;/code&gt; branch, you can configure a GitHub webhook that sends a POST request to your server (where this Python script is running) whenever a push event occurs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;GitHub Webhook Configuration:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to your GitHub repository settings.&lt;/li&gt;
&lt;li&gt;Under "Webhooks," click "Add webhook."&lt;/li&gt;
&lt;li&gt;Set the "Payload URL" to the URL of your server that runs the Python script.&lt;/li&gt;
&lt;li&gt;Choose &lt;code&gt;application/json&lt;/code&gt; as the content type.&lt;/li&gt;
&lt;li&gt;Set the events to listen for (e.g., &lt;code&gt;push&lt;/code&gt; events).&lt;/li&gt;
&lt;li&gt;Save the webhook.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Handling the Webhook:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You may need to set up a simple HTTP server using Flask, FastAPI, or a similar framework to handle the incoming webhook requests from GitHub and trigger the Jenkins job accordingly.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;jsonify&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Jenkins server details
&lt;/span&gt;&lt;span class="n"&gt;jenkins_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;http://your-jenkins-server.com&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;job_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;your-job-name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;your-username&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;api_token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;your-api-token&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/webhook&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;POST&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;github_webhook&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;

    &lt;span class="c1"&gt;# Extract branch name and commit ID from the payload
&lt;/span&gt;    &lt;span class="n"&gt;branch_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ref&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# Get the branch name
&lt;/span&gt;    &lt;span class="n"&gt;commit_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;after&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="c1"&gt;# Only trigger the job if it's the main branch
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;branch_name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;main&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;job_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;jenkins_url&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/job/&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;job_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/buildWithParameters&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
        &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;BRANCH_NAME&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;branch_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;COMMIT_ID&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;commit_id&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;job_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;api_token&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;jsonify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Jenkins job triggered successfully.&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="mi"&gt;201&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;jsonify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Failed to trigger Jenkins job.&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;jsonify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;No action taken.&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;0.0.0.0&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Step 5: Deploying the Flask App&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Deploy this Flask app on a server and ensure it is accessible via the public internet, so GitHub's webhook can send data to it.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This example illustrates how Python can be integrated into a CI/CD pipeline, interacting with tools like Jenkins to automate essential tasks. &lt;/p&gt;

&lt;h3&gt;
  
  
  3.&lt;strong&gt;Configuration Management and Orchestration&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tooling:&lt;/strong&gt; Ansible, Chef, Puppet&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example:&lt;/strong&gt; Using Python scripts with Ansible to manage the configuration of servers. Scripts can be used to ensure that all servers are configured consistently and to manage complex deployments that require orchestration of multiple services.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this example, we'll use Python to manage server configurations with Ansible. The script will run Ansible playbooks to ensure servers are configured consistently and orchestrate the deployment of multiple services.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Automating Server Configuration with Ansible and Python&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;br&gt;
You need to configure a set of servers to ensure they have the latest version of a web application, along with necessary dependencies and configurations. You want to use Ansible for configuration management and Python to trigger and manage Ansible playbooks.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Step 1: Create Ansible Playbooks&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;playbooks/setup.yml&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
This Ansible playbook installs necessary packages and configures the web server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="nn"&gt;---&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Configure web servers&lt;/span&gt;
  &lt;span class="na"&gt;hosts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;web_servers&lt;/span&gt;
  &lt;span class="na"&gt;become&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;yes&lt;/span&gt;
  &lt;span class="na"&gt;tasks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Install nginx&lt;/span&gt;
      &lt;span class="na"&gt;apt&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx&lt;/span&gt;
        &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;present&lt;/span&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy web application&lt;/span&gt;
      &lt;span class="na"&gt;copy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/path/to/local/webapp&lt;/span&gt;
        &lt;span class="na"&gt;dest&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/var/www/html/webapp&lt;/span&gt;
        &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;www-data&lt;/span&gt;
        &lt;span class="na"&gt;group&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;www-data&lt;/span&gt;
        &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;0644'&lt;/span&gt;

    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Ensure nginx is running&lt;/span&gt;
      &lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx&lt;/span&gt;
        &lt;span class="na"&gt;state&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;started&lt;/span&gt;
        &lt;span class="na"&gt;enabled&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;yes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;inventory/hosts&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
Define your servers in the Ansible inventory file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[web_servers]&lt;/span&gt;
&lt;span class="err"&gt;server1.example.com&lt;/span&gt;
&lt;span class="err"&gt;server2.example.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Step 2: Write the Python Script&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The Python script will use the &lt;code&gt;subprocess&lt;/code&gt; module to run Ansible commands and manage playbook execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run_ansible_playbook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playbook_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;inventory_path&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Run an Ansible playbook using the subprocess module.

    :param playbook_path: Path to the Ansible playbook file.
    :param inventory_path: Path to the Ansible inventory file.
    :return: None
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ansible-playbook&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-i&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;inventory_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;playbook_path&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PIPE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PIPE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Ansible playbook executed successfully.&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CalledProcessError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Ansible playbook execution failed.&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Paths to the playbook and inventory files
&lt;/span&gt;    &lt;span class="n"&gt;playbook_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;playbooks/setup.yml&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="n"&gt;inventory_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;inventory/hosts&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

    &lt;span class="c1"&gt;# Run the Ansible playbook
&lt;/span&gt;    &lt;span class="nf"&gt;run_ansible_playbook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;playbook_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;inventory_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Step 3: Explanation&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Ansible Playbook (&lt;code&gt;setup.yml&lt;/code&gt;):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tasks:&lt;/strong&gt; This playbook installs Nginx, deploys the web application, and ensures Nginx is running.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hosts:&lt;/strong&gt; &lt;code&gt;web_servers&lt;/code&gt; is a group defined in the inventory file.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Inventory File (&lt;code&gt;hosts&lt;/code&gt;):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Groups:&lt;/strong&gt; Defines which servers are part of the &lt;code&gt;web_servers&lt;/code&gt; group.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Python Script (&lt;code&gt;run_ansible_playbook&lt;/code&gt; function):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;subprocess.run&lt;/code&gt;:&lt;/strong&gt; Executes the &lt;code&gt;ansible-playbook&lt;/code&gt; command to apply configurations defined in the playbook.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling:&lt;/strong&gt; Catches and prints errors if the playbook execution fails.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Step 4: Running the Script&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Make sure Ansible is installed on the system where the Python script is running.&lt;/li&gt;
&lt;li&gt;Ensure the &lt;code&gt;ansible-playbook&lt;/code&gt; command is accessible in the system PATH.&lt;/li&gt;
&lt;li&gt;Execute the Python script to apply the Ansible configurations:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 your_script_name.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Step 5: Advanced Use Cases&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Inventory:&lt;/strong&gt; Use Python to generate dynamic inventory files based on real-time data from a database or an API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Role-based Configurations:&lt;/strong&gt; Define more complex configurations using Ansible roles and use Python to manage role-based deployments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Notifications and Logging:&lt;/strong&gt; Extend the Python script to send notifications (e.g., via email or Slack) or log detailed information about the playbook execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;By integrating Python with Ansible, you can automate server configuration and orchestration tasks efficiently. Python scripts can manage and trigger Ansible playbooks, ensuring that server configurations are consistent and deployments are orchestrated seamlessly. &lt;/p&gt;

&lt;h3&gt;
  
  
  4 &lt;strong&gt;Monitoring and Alerting with Python&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In a modern monitoring setup, you often need to collect metrics and logs from various services, analyze them, and push them to monitoring systems like Prometheus or Elasticsearch. Python can be used to gather and process this data, and set up automated alerts based on specific conditions.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Collecting Metrics and Logs, and Setting Up Alerts&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Collecting Metrics and Logs&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;br&gt;
You want to collect custom metrics and logs from your application and push them to Prometheus and Elasticsearch. Additionally, you'll set up automated alerts based on specific conditions.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Step 1: Collecting Metrics with Python and Prometheus&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;To collect and expose custom metrics from your application, you can use the &lt;code&gt;prometheus_client&lt;/code&gt; library in Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install &lt;code&gt;prometheus_client&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;prometheus_client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Python Script to Expose Metrics (&lt;code&gt;metrics_server.py&lt;/code&gt;):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;prometheus_client&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;start_http_server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Gauge&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;

&lt;span class="c1"&gt;# Create a metric to track the number of requests
&lt;/span&gt;&lt;span class="n"&gt;REQUESTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Gauge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;app_requests_total&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Total number of requests processed by the application&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_request&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Simulate processing a request.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;REQUESTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# Increment the request count
&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Start up the server to expose metrics
&lt;/span&gt;    &lt;span class="nf"&gt;start_http_server&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Metrics will be available at http://localhost:8000/metrics
&lt;/span&gt;
    &lt;span class="c1"&gt;# Simulate processing requests
&lt;/span&gt;    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;process_request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uniform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Simulate random request intervals
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Step 2: Collecting Logs with Python and Elasticsearch&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;To push logs to Elasticsearch, you can use the &lt;code&gt;elasticsearch&lt;/code&gt; Python client.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install &lt;code&gt;elasticsearch&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;elasticsearch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Python Script to Send Logs (&lt;code&gt;log_collector.py&lt;/code&gt;):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;elasticsearch&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Elasticsearch&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;logging&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;

&lt;span class="c1"&gt;# Elasticsearch client setup
&lt;/span&gt;&lt;span class="n"&gt;es&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Elasticsearch&lt;/span&gt;&lt;span class="p"&gt;([{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;host&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;localhost&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;port&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9200&lt;/span&gt;&lt;span class="p"&gt;}])&lt;/span&gt;
&lt;span class="n"&gt;index_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;application-logs&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;# Configure Python logging
&lt;/span&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;basicConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;INFO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;log_collector&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;log_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Log a message and send it to Elasticsearch.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;es&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;index_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;timestamp&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()})&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;log_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;This is a sample log message.&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Log every 5 seconds
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Step 3: Setting Up Alerts&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;To set up alerts, you need to define alerting rules based on the metrics and logs collected. Here’s an example of how you can configure alerts with Prometheus.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prometheus Alerting Rules (&lt;code&gt;prometheus_rules.yml&lt;/code&gt;):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;groups&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;example_alerts&lt;/span&gt;
  &lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;alert&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;HighRequestRate&lt;/span&gt;
    &lt;span class="na"&gt;expr&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rate(app_requests_total[1m]) &amp;gt; &lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;
    &lt;span class="na"&gt;for&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;2m&lt;/span&gt;
    &lt;span class="na"&gt;labels&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;severity&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;critical&lt;/span&gt;
    &lt;span class="na"&gt;annotations&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;summary&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;High&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;request&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;rate&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;detected"&lt;/span&gt;
      &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Request&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;rate&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;is&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;above&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;5&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;requests&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;per&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;minute&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;for&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;the&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;last&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;2&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;minutes."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Deploying Alerts:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Update Prometheus Configuration:&lt;/strong&gt;
Ensure that your Prometheus server is configured to load the alerting rules file. Update your &lt;code&gt;prometheus.yml&lt;/code&gt; configuration file:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;   &lt;span class="na"&gt;rule_files&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
     &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;prometheus_rules.yml'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reload Prometheus Configuration:&lt;/strong&gt;
After updating the configuration, reload Prometheus to apply the new rules.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;kill&lt;/span&gt; &lt;span class="nt"&gt;-HUP&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;pgrep prometheus&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Grafana Setup:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Add Prometheus as a Data Source:&lt;/strong&gt;&lt;br&gt;
Go to Grafana's data source settings and add Prometheus.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create Dashboards:&lt;/strong&gt;&lt;br&gt;
Create dashboards in Grafana to visualize the metrics exposed by your application. You can set up alerts in Grafana as well, based on the metrics from Prometheus.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Elasticsearch Alerting:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Install Elastic Stack Alerting Plugin:&lt;/strong&gt;&lt;br&gt;
If you're using Elasticsearch with Kibana, you can use Kibana's alerting features to create alerts based on log data. You can set thresholds and get notifications via email, Slack, or other channels.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Define Alert Conditions:&lt;/strong&gt;&lt;br&gt;
Use Kibana to define alert conditions based on your log data indices.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;By using Python scripts to collect and process metrics and logs, and integrating them with tools like Prometheus and Elasticsearch, you can create a robust monitoring and alerting system. The examples provided show how to expose custom metrics, push logs, and set up alerts for various conditions. This setup ensures you can proactively monitor your application, respond to issues quickly, and maintain system reliability.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;Use Case: Scripting for Routine Tasks and Maintenance&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Routine maintenance tasks like backups, system updates, and log rotation are essential for keeping your infrastructure healthy. You can automate these tasks using Python scripts and schedule them with cron jobs. Below are examples of Python scripts for common routine maintenance tasks and how to set them up with cron.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Python Scripts for Routine Tasks&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Backup Script&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;br&gt;
Create a Python script to back up a directory to a backup location. This script will be scheduled to run daily to ensure that your data is regularly backed up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Backup Script (&lt;code&gt;backup_script.py&lt;/code&gt;):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;shutil&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;

&lt;span class="c1"&gt;# Define source and backup directories
&lt;/span&gt;&lt;span class="n"&gt;source_dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/path/to/source_directory&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;backup_dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/path/to/backup_directory&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;# Create a timestamped backup file name
&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;strftime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;%Y%m%d-%H%M%S&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;backup_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;backup_dir&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/backup_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.tar.gz&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_backup&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Create a backup of the source directory.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;shutil&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make_archive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;backup_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;.tar.gz&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;''&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;gztar&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;source_dir&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Backup created at &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;backup_file&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;create_backup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;2. System Update Script&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;br&gt;
Create a Python script to update the system packages. This script will ensure that the system is kept up-to-date with the latest security patches and updates.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;System Update Script (&lt;code&gt;system_update.py&lt;/code&gt;):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update_system&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Update the system packages.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;sudo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apt-get&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;update&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;sudo&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apt-get&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;upgrade&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-y&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;System updated successfully.&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CalledProcessError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Failed to update the system: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;update_system&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;3. Log Rotation Script&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;br&gt;
Create a Python script to rotate log files, moving old logs to an archive directory and compressing them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Log Rotation Script (&lt;code&gt;log_rotation.py&lt;/code&gt;):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;shutil&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;

&lt;span class="c1"&gt;# Define log directory and archive directory
&lt;/span&gt;&lt;span class="n"&gt;log_dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/path/to/log_directory&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;archive_dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/path/to/archive_directory&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;rotate_logs&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Rotate log files by moving and compressing them.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;log_file&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;log_dir&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;log_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;log_dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;log_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;log_path&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;strftime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;%Y%m%d-%H%M%S&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;archive_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;archive_dir&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;log_file&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.gz&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
            &lt;span class="n"&gt;shutil&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;log_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;archive_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;shutil&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make_archive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;archive_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;.gz&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;''&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;gztar&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;root_dir&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;archive_dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;base_dir&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;log_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;log_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Log rotated: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;archive_file&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;rotate_logs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Setting Up Cron Jobs&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You need to set up cron jobs to schedule these scripts to run at specific intervals. Use the &lt;code&gt;crontab&lt;/code&gt; command to edit the cron schedule.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Open the Crontab File:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   crontab &lt;span class="nt"&gt;-e&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Add Cron Job Entries:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Daily Backup at 2 AM:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; 0 2 &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; /usr/bin/python3 /path/to/backup_script.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Weekly System Update on Sunday at 3 AM:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; 0 3 &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; 0 /usr/bin/python3 /path/to/system_update.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Log Rotation Every Day at Midnight:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; 0 0 &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; /usr/bin/python3 /path/to/log_rotation.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0 2 * * *&lt;/code&gt;: Runs the script at 2:00 AM every day.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0 3 * * 0&lt;/code&gt;: Runs the script at 3:00 AM every Sunday.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0 0 * * *&lt;/code&gt;: Runs the script at midnight every day.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Using Python scripts for routine tasks and maintenance helps automate critical processes such as backups, system updates, and log rotation. By scheduling these scripts with cron jobs, you ensure that these tasks are performed consistently and without manual intervention. This approach enhances the reliability and stability of your infrastructure, keeping it healthy and up-to-date.&lt;/p&gt;

</description>
      <category>python</category>
      <category>devops</category>
      <category>api</category>
      <category>learning</category>
    </item>
    <item>
      <title>SDLC , Agile vs DevOps</title>
      <dc:creator>Pratik Nalawade</dc:creator>
      <pubDate>Tue, 23 Jul 2024 05:13:48 +0000</pubDate>
      <link>https://dev.to/pratik_nalawade/sdlc-agile-vs-devops-47a2</link>
      <guid>https://dev.to/pratik_nalawade/sdlc-agile-vs-devops-47a2</guid>
      <description>&lt;p&gt;Software Development Life Cycle (SDLC) and DevOps are both methodologies used in the software development process, but they focus on different aspects and have distinct goals. Here's a comparison:&lt;/p&gt;

&lt;h3&gt;
  
  
  SDLC (Software Development Life Cycle)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40u8cyluag1cc4ta60lr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40u8cyluag1cc4ta60lr.png" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SDLC is a structured process that defines the stages involved in the development of software applications.&lt;/li&gt;
&lt;li&gt;It ensures that the software meets business requirements and is of high quality.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Phases&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Planning&lt;/strong&gt;: Define objectives, scope, resources, and schedule.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Requirement Analysis&lt;/strong&gt;: Gather and analyze business and user requirements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Design&lt;/strong&gt;: Create the architecture and design of the software.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Implementation&lt;/strong&gt;: Write the code according to the design.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testing&lt;/strong&gt;: Test the software for defects and issues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deployment&lt;/strong&gt;: Release the software to production.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintenance&lt;/strong&gt;: Provide ongoing support and bug fixes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Focus&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Emphasizes detailed planning and linear progression through distinct phases.&lt;/li&gt;
&lt;li&gt;Ensures that each phase is completed before moving to the next.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Approach&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Traditional and often follows a waterfall or sequential model.&lt;/li&gt;
&lt;li&gt;Can also follow iterative models like Agile, where each iteration includes all phases of the SDLC.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  DevOps (Development and Operations)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq643ndapj5iz8zwqaubk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq643ndapj5iz8zwqaubk.png" alt="Image description" width="800" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DevOps is a set of practices that combines software development (Dev) and IT operations (Ops).&lt;/li&gt;
&lt;li&gt;Aims to shorten the development lifecycle and provide continuous delivery with high software quality.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Principles&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Collaboration&lt;/strong&gt;: Close collaboration between development and operations teams.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automation&lt;/strong&gt;: Automate repetitive tasks to increase efficiency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Continuous Integration (CI)&lt;/strong&gt;: Frequently integrate code changes into a shared repository.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Continuous Delivery (CD)&lt;/strong&gt;: Continuously deliver code to production.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitoring and Feedback&lt;/strong&gt;: Continuously monitor applications and provide feedback for improvements.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Focus&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Emphasizes a culture of collaboration and shared responsibility.&lt;/li&gt;
&lt;li&gt;Aims to deliver features, fixes, and updates frequently and reliably.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Approach&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses tools and practices such as CI/CD pipelines, infrastructure as code, automated testing, and configuration management.&lt;/li&gt;
&lt;li&gt;Promotes iterative improvements and rapid deployment cycles.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Key Differences
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SDLC&lt;/strong&gt;: Focuses on the systematic development and maintenance of software.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps&lt;/strong&gt;: Focuses on speed, collaboration, and continuous improvement in both development and operations.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Phases vs. Practices&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SDLC&lt;/strong&gt;: Structured phases with a clear sequence.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps&lt;/strong&gt;: Set of practices and tools integrated into the workflow.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Team Structure&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SDLC&lt;/strong&gt;: Distinct roles for development and operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps&lt;/strong&gt;: Blurred lines between development and operations, promoting cross-functional teams.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Delivery&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SDLC&lt;/strong&gt;: Often results in longer release cycles.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps&lt;/strong&gt;: Enables faster and more frequent releases.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The Impact of DevOps on SDLC
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Increased Collaboration&lt;/strong&gt;: DevOps fosters collaboration between development, operations, and other cross-functional teams. By breaking down silos and promoting shared responsibilities, DevOps encourages teams to work together towards common goals.&lt;br&gt;
Faster Delivery: Automation and CI/CD pipelines enable faster and more frequent delivery of software updates. This accelerates the pace of innovation and allows organizations to respond quickly to changing market demands.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Improved Quality&lt;/strong&gt;: DevOps practices such as automated testing, continuous monitoring, and feedback loops enhance the overall quality of software. By detecting and addressing issues early in the development process, teams can deliver more reliable and resilient applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enhanced Scalability&lt;/strong&gt;: DevOps principles like IaC and containerization facilitate the scalability of infrastructure and applications. This allows organizations to efficiently manage growth and handle fluctuations in demand without compromising performance or reliability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Greater Agility&lt;/strong&gt;: DevOps promotes agility by enabling rapid iterations, experimentation, and adaptation. Teams can quickly pivot in response to customer feedback, market trends, or competitive pressures, ensuring continued relevance and competitiveness.&lt;/p&gt;

&lt;p&gt;Agile and DevOps are both methodologies that aim to improve software development processes, but they focus on different aspects and principles. Here’s a detailed comparison:&lt;/p&gt;

&lt;h3&gt;
  
  
  Agile
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Agile is a set of principles and practices for software development under which requirements and solutions evolve through collaborative effort.&lt;/li&gt;
&lt;li&gt;It promotes flexible responses to change and iterative progress through short development cycles called sprints.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Principles&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Customer Collaboration&lt;/strong&gt;: Engage customers throughout the development process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterative Development&lt;/strong&gt;: Deliver small, incremental updates frequently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexible and Adaptive Planning&lt;/strong&gt;: Respond to changes rather than following a rigid plan.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Self-Organizing Teams&lt;/strong&gt;: Empower teams to make decisions and manage their work.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Focus&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Emphasizes adaptability, customer feedback, and small, frequent releases.&lt;/li&gt;
&lt;li&gt;Aims to improve customer satisfaction through continuous delivery of valuable software.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Methodologies&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Common frameworks include Scrum, Kanban, Lean, and Extreme Programming (XP).&lt;/li&gt;
&lt;li&gt;Iterations typically last 1-4 weeks, with regular retrospectives and reviews.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Team Structure&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cross-functional teams that include developers, testers, and business analysts.&lt;/li&gt;
&lt;li&gt;Roles such as Scrum Master and Product Owner are key in Agile frameworks like Scrum.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  DevOps
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DevOps is a set of practices that combines software development (Dev) and IT operations (Ops) to shorten the development lifecycle and deliver high-quality software continuously.&lt;/li&gt;
&lt;li&gt;It aims to create a culture of collaboration between development and operations teams.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Principles&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Collaboration and Communication&lt;/strong&gt;: Foster close collaboration between development, operations, and other stakeholders.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automation&lt;/strong&gt;: Automate repetitive tasks to increase efficiency and reduce errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Continuous Integration (CI)&lt;/strong&gt;: Frequently merge code changes into a central repository.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Continuous Delivery (CD)&lt;/strong&gt;: Continuously deploy code to production environments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitoring and Feedback&lt;/strong&gt;: Continuously monitor applications and provide feedback for improvements.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Focus&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Emphasizes the automation of processes, continuous delivery, and the integration of development and operations.&lt;/li&gt;
&lt;li&gt;Aims to improve the speed, efficiency, and reliability of software delivery.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Practices and Tools&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses tools for CI/CD (e.g., Jenkins, GitLab CI), configuration management (e.g., Ansible, Puppet), and infrastructure as code (e.g., Terraform).&lt;/li&gt;
&lt;li&gt;Promotes practices like version control, automated testing, and continuous monitoring.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Team Structure&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blends development and operations roles to create cross-functional teams.&lt;/li&gt;
&lt;li&gt;Encourages shared responsibilities for deployment and maintenance tasks.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Key Differences
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Scope&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Agile&lt;/strong&gt;: Primarily focuses on the development process and managing changes in requirements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps&lt;/strong&gt;: Encompasses the entire software lifecycle, from development to operations and maintenance.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Agile&lt;/strong&gt;: Aims to deliver small, incremental changes quickly and efficiently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps&lt;/strong&gt;: Aims to automate and integrate the processes between software development and IT operations to enable continuous delivery.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Practices and Tools&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Agile&lt;/strong&gt;: Focuses on methodologies and frameworks (Scrum, Kanban) and practices like daily stand-ups, sprints, and retrospectives.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps&lt;/strong&gt;: Focuses on automation and tools for CI/CD, configuration management, and monitoring.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Team Dynamics&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Agile&lt;/strong&gt;: Teams are usually small and focused on development, with roles like Scrum Master and Product Owner.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps&lt;/strong&gt;: Teams are cross-functional, including both development and operations, with a focus on collaboration and shared responsibilities.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frx11jz5tgfv0e3i904ac.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frx11jz5tgfv0e3i904ac.png" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Integration of Agile and DevOps
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Agile and DevOps can be integrated to complement each other:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Agile&lt;/strong&gt; can be used to manage and deliver small, incremental changes efficiently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DevOps&lt;/strong&gt; can ensure that these changes are deployed quickly and reliably, with a focus on automation and continuous delivery.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;In summary, while Agile focuses on improving the development process through iterative progress and customer collaboration, DevOps extends this improvement to the entire software lifecycle by promoting automation, continuous delivery, and close collaboration between development and operations teams.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>agile</category>
      <category>sdlc</category>
    </item>
  </channel>
</rss>
