<?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>Forem: Christopher Akinsanmi</title>
    <description>The latest articles on Forem by Christopher Akinsanmi (@swapdevs).</description>
    <link>https://forem.com/swapdevs</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%2F603945%2F401c8b9d-6adf-4a65-b1f0-31ee0d0cb67f.jpg</url>
      <title>Forem: Christopher Akinsanmi</title>
      <link>https://forem.com/swapdevs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/swapdevs"/>
    <language>en</language>
    <item>
      <title>"Help! My Server is on Fire (Probably)" - A DevOps Engineer's Guide to Panic-Driven Statistics.</title>
      <dc:creator>Christopher Akinsanmi</dc:creator>
      <pubDate>Wed, 16 Apr 2025 09:19:02 +0000</pubDate>
      <link>https://forem.com/swapdevs/help-my-server-is-on-fire-probably-a-devops-engineers-guide-to-panic-driven-statistics-4o8j</link>
      <guid>https://forem.com/swapdevs/help-my-server-is-on-fire-probably-a-devops-engineers-guide-to-panic-driven-statistics-4o8j</guid>
      <description>&lt;h2&gt;
  
  
  The Problem: Servers Behaving Badly
&lt;/h2&gt;

&lt;p&gt;You wake up at 3 AM to an alert: "CPU at 99%!" Your heart rate spikes. Is it a crypto miner? A runaway cron job? Did someone deploy to production on a Friday? Again?&lt;br&gt;
As a DevOps engineer, you need answers—fast. But logging into a server and running a dozen commands is exhausting. You need one magical script to tell you everything:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How much CPU is melting?&lt;/li&gt;
&lt;li&gt;Why is memory disappearing like my motivation on Monday?&lt;/li&gt;
&lt;li&gt;Who filled up the disk? (Spoiler: It’s always logs.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, I present to you: server-stats.sh—the Swiss Army knife of server diagnostics, wrapped in anxiety-reducing colors.&lt;/p&gt;
&lt;h2&gt;
  
  
  Introducing server-stats.sh – Because Guessing is Not a Monitoring Strategy
&lt;/h2&gt;

&lt;p&gt;This script does everything you wish &lt;em&gt;top&lt;/em&gt; and &lt;em&gt;htop&lt;/em&gt; did, but without the weird keybindings you always forget.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What It Tells You (Before You Have a Mental Breakdown)&lt;/strong&gt;&lt;br&gt;
"Is the CPU on Fire?"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Total CPU usage (so you know whether to panic now or in 5 minutes).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"Why is Memory Gone?"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Free vs. Used (with percentages, because math is hard at 3 AM).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"Who Ate My Disk Space?"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lists disk usage (and turns red if it’s critical, just like your face when you see 95% usage).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"Top 5 Processes Trying to Kill Your Server"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CPU hogs (looking at you, Java).&lt;/li&gt;
&lt;li&gt;Memory vampires (probably Node.js with 500 open dependencies).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bonus: "Is Anyone Else Even Awake?"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shows logged-in users (so you know who to blame).&lt;/li&gt;
&lt;li&gt;Failed login attempts (because hackers love weekends too).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Script (AKA "The Hero We Need")&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;#!/bin/bash

# server-stats.sh - A comprehensive server performance statistics script

# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# Function to display section headers
section_header() {
    echo -e "\n${MAGENTA}=== $1 ===${NC}"
}

# Function to convert bytes to human readable format
human_readable() {
    local bytes=$1
    local units=("B" "KB" "MB" "GB" "TB" "PB")
    local unit=0

    while (( bytes &amp;gt; 1024 )) &amp;amp;&amp;amp; (( unit &amp;lt; ${#units[@]} - 1 )); do
        bytes=$((bytes / 1024))
        ((unit++))
    done

    echo "$bytes ${units[$unit]}"
}

# Get OS information
section_header "System Information"
echo -e "${CYAN}Hostname:${NC} $(hostname)"
echo -e "${CYAN}OS:${NC} $(grep PRETTY_NAME /etc/os-release | cut -d '"' -f 2)"
echo -e "${CYAN}Kernel:${NC} $(uname -r)"
echo -e "${CYAN}Uptime:${NC} $(uptime -p | sed 's/up //')"
echo -e "${CYAN}Current Time:${NC} $(date)"

# CPU Usage
section_header "CPU Usage"
total_cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
echo -e "${CYAN}Total CPU Usage:${NC} $total_cpu_usage"
load_avg=$(uptime | awk -F 'load average: ' '{print $2}')
echo -e "${CYAN}Load Average (1, 5, 15 min):${NC} $load_avg"

# Memory Usage
section_header "Memory Usage"
free_output=$(free -m)
total_mem=$(echo "$free_output" | grep Mem: | awk '{print $2}')
used_mem=$(echo "$free_output" | grep Mem: | awk '{print $3}')
free_mem=$(echo "$free_output" | grep Mem: | awk '{print $4}')
mem_percent=$((used_mem * 100 / total_mem))

echo -e "${CYAN}Total Memory:${NC} ${total_mem}MB"
echo -e "${CYAN}Used Memory:${NC} ${used_mem}MB (${mem_percent}%)"
echo -e "${CYAN}Free Memory:${NC} ${free_mem}MB"

# Swap Usage
swap_total=$(echo "$free_output" | grep Swap: | awk '{print $2}')
swap_used=$(echo "$free_output" | grep Swap: | awk '{print $3}')
if [ "$swap_total" -gt 0 ]; then
    swap_percent=$((swap_used * 100 / swap_total))
    echo -e "${CYAN}Total Swap:${NC} ${swap_total}MB"
    echo -e "${CYAN}Used Swap:${NC} ${swap_used}MB (${swap_percent}%)"
else
    echo -e "${CYAN}Swap:${NC} Not configured"
fi

# Disk Usage
section_header "Disk Usage"
df_output=$(df -h --total | grep -vE '^tmpfs|udev')
echo "$df_output" | awk -v cyan="${CYAN}" -v nc="${NC}" -v green="${GREEN}" -v red="${RED}" '
    NR==1 {printf "%s%-20s %s%-10s %s%-10s %s%-10s %s%-10s %s%s\n", cyan, "Filesystem", cyan, "Size", cyan, "Used", cyan, "Avail", cyan, "Use%", cyan, "Mounted on", nc}
    NR&amp;gt;1 &amp;amp;&amp;amp; $1 != "total" {
        color=($5+0 &amp;gt; 80) ? red : green;
        printf "%-20s %-10s %-10s %-10s %s%-10s%s %-10s\n", $1, $2, $3, $4, color, $5, nc, $6
    }
    $1 == "total" {
        printf "%s%-20s %s%-10s %s%-10s %s%-10s %s%-10s %s%s\n", cyan, $1, cyan, $2, cyan, $3, cyan, $4, cyan, $5, cyan, $6, nc
    }'

# Top Processes by CPU
section_header "Top 5 Processes by CPU Usage"
echo -e "${CYAN}%-10s %-10s %-10s %-10s %s${NC}" "PID" "USER" "CPU%" "MEM%" "COMMAND"
ps -eo pid,user,%cpu,%mem,comm --sort=-%cpu | head -n 6 | tail -n 5 | awk '{printf "%-10s %-10s %-10s %-10s %s\n", $1, $2, $3, $4, $5}'

# Top Processes by Memory
section_header "Top 5 Processes by Memory Usage"
echo -e "${CYAN}%-10s %-10s %-10s %-10s %s${NC}" "PID" "USER" "MEM%" "CPU%" "COMMAND"
ps -eo pid,user,%mem,%cpu,comm --sort=-%mem | head -n 6 | tail -n 5 | awk '{printf "%-10s %-10s %-10s %-10s %s\n", $1, $2, $3, $4, $5}'

# User Information
section_header "User Information"
echo -e "${CYAN}Logged in Users:${NC}"
who
echo -e "\n${CYAN}Failed Login Attempts (last 24h):${NC}"
lastb -a --time-format iso | head -n 10

# Network Information
section_header "Network Information"
echo -e "${CYAN}Active Connections:${NC}"
ss -tulnp | head -n 10

# Security Information
section_header "Security Information"
echo -e "${CYAN}Last 10 Security Events:${NC}"
grep -i 'error\|fail\|denied' /var/log/auth.log | tail -n 10 2&amp;gt;/dev/null || echo "No security logs found or permission denied"

echo -e "\n${GREEN}Server stats collected successfully!${NC}"

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to Use This Script (Before You Cry)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Save it as server-stats.sh&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;nano server-stats.sh  # Paste the script, then [Ctrl]+[X], [Y], [Enter]&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Make it executable (because permissions are annoying)&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;chmod +x server-stats.sh&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Run it when you suspect disaster&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;./server-stats.sh&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Interpret the results&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Green = "Maybe I can go back to bed?"&lt;br&gt;
Yellow = "I should check this… later."&lt;br&gt;
Red = "Wake up the team (and maybe a therapist)."&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Servers are like pets—they misbehave when you least expect it. But with &lt;em&gt;server-stats.sh&lt;/em&gt;, you can:&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Diagnose problems in seconds&lt;/strong&gt; (instead of Googling &lt;em&gt;htop&lt;/em&gt; commands).&lt;br&gt;
✅ &lt;strong&gt;Blame the right processes&lt;/strong&gt; (looking at you, &lt;em&gt;apache2&lt;/em&gt;).&lt;br&gt;
✅ &lt;strong&gt;Pretend you’re a hacker&lt;/strong&gt; (even though you just ran a Bash script).&lt;/p&gt;

&lt;p&gt;So next time your server acts up, &lt;strong&gt;stay calm, run the script, and panic efficiently&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;🚀 &lt;strong&gt;Happy debugging&lt;/strong&gt;! (And may your uptime be high.)&lt;/p&gt;

</description>
      <category>devops</category>
      <category>linux</category>
      <category>automation</category>
      <category>archlinux</category>
    </item>
  </channel>
</rss>
