Latest / CyberCode Academy / Course 4 - Learning Linux Shell Scripting | Episode 8: System Monitoring, Performance Measurement, and Log Management
Transcript
- 0:00Speaker 1: Welcome to the deep dive. If you're building a running,
- 0:03Speaker 1: well pretty much any piece of modern technology, you know,
- 0:05Speaker 1: the real test isn't when things are going smoothly, it's
- 0:08Speaker 1: when something breaks exactly. So our mission today is to
- 0:13Speaker 1: strap on the monitor's cap. We're going to shortcut your
- 0:16Speaker 1: journey into system monitoring by showing you the essential techniques
- 0:19Speaker 1: for really looking under.
- 0:20Speaker 2: The hood, right, the powerful commands, and the logic that
- 0:23Speaker 2: turns all that raw data into something you can actually.
- 0:26Speaker 1: Use actionable intelligence instantly.
- 0:28Speaker 2: Yeah, think of an operating system as this huge, intricate machine.
- 0:33Speaker 2: If one little gear slips, you need to know about
- 0:35Speaker 2: it immediately, and your first line of defense is always
- 0:38Speaker 2: logging the timeline. It's the forensic timeline. You're just meticulously
- 0:43Speaker 2: recording system events into files, so if an app crashes,
- 0:46Speaker 2: the logs are the only thing that lets you kind
- 0:48Speaker 2: of rewind and figure out what happened.
- 0:50Speaker 1: We've broken this deep dive into three critical phases. First,
- 0:54Speaker 1: we're starting with resource management, so understanding disk space and
- 0:58Speaker 1: measuring how efficient your commands.
- 1:00Speaker 2: Then we'll get into tracking system and user activity. You
- 1:02Speaker 2: know who's logged in and what resources they're eating up.
- 1:05Speaker 1: And finally we'll tackle log automation and security, which is
- 1:09Speaker 1: how you manage the monster log files that you end
- 1:11Speaker 1: up creating. Okay, let's start with the most basic and
- 1:17Speaker 1: probably the most common crisis, running out of disk space.
- 1:21Speaker 2: Everyone's been there.
- 1:22Speaker 1: Yeah, the server starts screaming that the disc is almost full.
- 1:27Speaker 1: Where do you even begin? Do you check the whole
- 1:29Speaker 1: capacity first or do you start digging into specific folders?
- 1:32Speaker 2: You really need both. And that's the power of this
- 1:35Speaker 2: dynamic duo DF and d U DF and du right
- 1:38Speaker 2: DF is disc free. It gives you the big picture
- 1:41Speaker 2: how much space is left on all your mounted filesystems.
- 1:45Speaker 2: It's all about capacity and du du is disc usage.
- 1:49Speaker 2: That's your magnifying glass. Lets you drill down and see
- 1:52Speaker 2: exactly how much space individual files or directories are consuming.
- 1:55Speaker 1: Okay, but I think we can all agree that seeing
- 1:57Speaker 1: file size is listed in like rawbites is completely useless
- 2:01Speaker 1: when you're in a hurry.
- 2:01Speaker 2: Oh it's the worst.
- 2:02Speaker 1: So what's the simple fix for that.
- 2:04Speaker 2: It's the h flag, the human readable flag. It's an
- 2:07Speaker 2: absolute must use. It just converts that mess of bytes
- 2:10Speaker 2: into killobikes, megabytes or gigabytes. You know, something you can
- 2:13Speaker 2: actually read.
- 2:14Speaker 1: Without it, you're just doing math in your head exactly.
- 2:16Speaker 1: So the real power here isn't just running DU on
- 2:18Speaker 1: one folder. It's about finding, say, the ten biggest space
- 2:23Speaker 1: hogs on the entire system. And that takes more than
- 2:26Speaker 1: just one command.
- 2:27Speaker 2: Right it does. It requires what we call command chaining,
- 2:30Speaker 2: and this is, you know, the defining skill of any
- 2:32Speaker 2: sophisticated system analysis. Yeah, so you'd run DU, but with
- 2:37Speaker 2: the A option to make sure you get the sizes
- 2:40Speaker 2: of all files, not just the directory.
- 2:42Speaker 1: Okay, so you get everything everything.
- 2:44Speaker 2: Then you pipe that entire output into the sort command.
- 2:47Speaker 2: You tell it to sort numerically and in reverse based on.
- 2:50Speaker 1: That size column, so biggest on top.
- 2:52Speaker 2: Biggest on top, and then finally you pipe that result
- 2:55Speaker 2: into head to just grab the top ten. It's an
- 2:58Speaker 2: incredibly efficient way to try fage a disk space emergency.
- 3:02Speaker 1: Okay, so we've handled storage, but what about time? Performance
- 3:06Speaker 1: monitoring is just as critical. If you're testing a new
- 3:09Speaker 1: algorithm or comparing app builds, how do you clock them?
- 3:12Speaker 2: You use the time command. It's super simple but really powerful.
- 3:17Speaker 2: You just put the word time in front of any
- 3:19Speaker 2: command you want to measure, and.
- 3:20Speaker 1: What does tell you?
- 3:21Speaker 2: It gives you three different measurements. First is real time
- 3:25Speaker 2: or wall clock time. That's just the total time that
- 3:28Speaker 2: passed from start to finish, So.
- 3:29Speaker 1: Like if I use a physical.
- 3:30Speaker 2: Stopwatch, exactly, it includes any time spent waiting for IO
- 3:34Speaker 2: or for the CPU to become free. But then you
- 3:37Speaker 2: get the two CPU specific times. Okay, you get user time,
- 3:41Speaker 2: which is the CPU time spent running the process's own code,
- 3:44Speaker 2: you know, outside the kernel, and then system time, which
- 3:46Speaker 2: is the CPU time spent making system calls inside the kernel.
- 3:50Speaker 1: So that difference can tell you if your app is
- 3:52Speaker 1: calculation heavy or if it's constantly asking the OS for things.
- 3:56Speaker 2: Precisely, it gives you a much deeper insight into what
- 3:59Speaker 2: your program is actually doing.
- 4:00Speaker 1: All right, let's move on to phase two, tracking system
- 4:03Speaker 1: and user activity. We need tools that give us a
- 4:06Speaker 1: live snapshot of the system's health, especially its load.
- 4:10Speaker 2: For a really quick check on who's logged in, you
- 4:12Speaker 2: can use the basic WHO command. It's fine, but just
- 4:14Speaker 2: the basics, Just the basics. For the full picture, you
- 4:17Speaker 2: want to run W. The W command shows you every
- 4:20Speaker 2: user where they logged in, from what terminal they're on.
- 4:23Speaker 2: But the first line is the.
- 4:24Speaker 1: Gold mine, and what's In that first line.
- 4:26Speaker 2: It gives you the current time, the total system uptime,
- 4:29Speaker 2: and most critically, the system load averages for the past one,
- 4:33Speaker 2: five and fifteen minutes.
- 4:35Speaker 1: I always get a kick out of the old school terms.
- 4:37Speaker 1: The w output mentions a TTY. What's the story there, right?
- 4:41Speaker 2: The ghost of admin past TTY stands for a teletypewriter.
- 4:46Speaker 2: It's a historical term, but it still identifies the device
- 4:48Speaker 2: file for a terminal session. It's a little reminder that
- 4:51Speaker 2: our modern virtual terminals came from, you know, actual physical
- 4:54Speaker 2: printing machines.
- 4:55Speaker 1: And for just the machines uptime you.
- 4:57Speaker 2: Can use the uptime command itself, super simple, and for
- 5:00Speaker 2: historical logins like who logged in yesterday or when the
- 5:03Speaker 2: last reboot was, use the last command. It reads all
- 5:07Speaker 2: that data from the varlav two file.
- 5:09Speaker 1: Speaking of performance, how do we hunt down CPU hogs?
- 5:13Speaker 1: A single run of the PS command, which grabs process details.
- 5:17Speaker 1: That's just one snapshot in time, isn't it.
- 5:19Speaker 2: That's a brilliant point. If you only run PS once,
- 5:22Speaker 2: you're going to miss process that spike and then die down, so.
- 5:24Speaker 1: You can't rely on it for an average, not at all.
- 5:26Speaker 2: This is where we need that chaining strategy. Again, the
- 5:29Speaker 2: really powerful way to do this is to wrap the
- 5:31Speaker 2: PS command in a shell loop and just log the
- 5:34Speaker 2: stats repeatedly, say every sixty seconds for an hour.
- 5:37Speaker 1: So you end up with sixty different sets of raw data.
- 5:40Speaker 1: How do you possibly aggregate that?
- 5:42Speaker 2: Use a specialized tool called awk.
- 5:45Speaker 1: Awk.
- 5:46Speaker 2: Yeah, it's a pattern standing and text processing language, and
- 5:49Speaker 2: it's perfect for handling data in columns like the output
- 5:52Speaker 2: from PS.
- 5:52Speaker 1: So you just feed it all sixty files.
- 5:54Speaker 2: You feed them all into AWK, and you tell it
- 5:56Speaker 2: to sum up the total CPU percentage used by each
- 6:00Speaker 2: unique process name across all those entries. Once it has
- 6:03Speaker 2: the totals, you just sort the results and piping through
- 6:05Speaker 2: ahead to get your top ten.
- 6:07Speaker 1: So you turn sixty snapshots into one single comprehensive report.
- 6:11Speaker 2: Exactly. It's a historical average, not just a snapshot.
- 6:15Speaker 1: That's fantastic. Now, what if you don't need historical data,
- 6:18Speaker 1: but you need like a real time dashboard for just
- 6:21Speaker 1: one thing, like watching a log file grow.
- 6:24Speaker 2: That's the perfect use case for the watch command. It's simple,
- 6:27Speaker 2: it's elegant. You give it a command and it just
- 6:29Speaker 2: runs it over and over again. How often The default
- 6:31Speaker 2: is every two seconds, but you can change that with
- 6:34Speaker 2: the man option. But the most useful flag, especially for logs,
- 6:38Speaker 2: is m D m D four differences. It highlights the
- 6:42Speaker 2: parts of the output that change between each update. It
- 6:45Speaker 2: makes any changes just pop right out at you.
- 6:47Speaker 1: Okay, let's get into phase three, which is all about
- 6:50Speaker 1: logging insecurity. Let's say you're monitoring sensitive files for tampering.
- 6:54Speaker 1: How do you log every single access, every modification, every deletion.
- 6:58Speaker 2: For that, you use a note weight. It's part of
- 7:01Speaker 2: the Notified tools package, and it's powerful because it lets
- 7:04Speaker 2: you monitor these specific events in real time.
- 7:07Speaker 1: And what kinds of events are we talking about.
- 7:09Speaker 2: Here, well, all the crucial ones you can track, create, delete,
- 7:13Speaker 2: modify when the content changes, access, so even just to.
- 7:15Speaker 1: Read and move, and you can just leave it running. Yep.
- 7:19Speaker 2: If you're setting up a security trap, you'd probably use
- 7:22Speaker 2: the SM option for continuous monitoring and air R to
- 7:25Speaker 2: recursively watch an entire directory and everything inside.
- 7:28Speaker 1: It to keep things orderly. Applications need a standard way
- 7:32Speaker 1: to write their system level logs like authentication failures or reboots.
- 7:36Speaker 1: What manages all that?
- 7:38Speaker 2: That would be the cislog demon and the syslog protocol.
- 7:41Speaker 2: It provides that standardized format and usually stores everything centrally
- 7:45Speaker 2: in the varlog directory.
- 7:46Speaker 1: Can I write my own messages to that log from
- 7:49Speaker 1: a script? Maybe?
- 7:50Speaker 2: Absolutely, That's what the logger command is for you, as
- 7:53Speaker 2: a user or a script writer, can write your own
- 7:55Speaker 2: custom messages right into the system log. You can even
- 7:58Speaker 2: use a tag string to help cislog route your mind
- 8:00Speaker 2: to a specific log file if you have it configured
- 8:02Speaker 2: that way.
- 8:03Speaker 1: And since we know these logs will just grow and
- 8:05Speaker 1: grow forever, how do we stop them from eating all
- 8:08Speaker 1: our disk space?
- 8:09Speaker 2: We use the essential utility loggertate. This is non negotiable
- 8:13Speaker 2: for system stability. It limits log file size and archives
- 8:17Speaker 2: the old entries for you automatically. You just have to
- 8:19Speaker 2: tell it what to do. So first you set the
- 8:21Speaker 2: trigger maybe size one meter, to tell it when to act.
- 8:24Speaker 1: And then you manage the old ones. How do you
- 8:26Speaker 1: tell it how much history to keep?
- 8:28Speaker 2: You use the rotate directive, so if you say rotate five,
- 8:31Speaker 2: the system keeps five of the older archived copies. You
- 8:34Speaker 2: also want to add compress, which usually uses g zip
- 8:37Speaker 2: to shrink those old logs down before archiving them, and.
- 8:40Speaker 1: You can do it based on time too.
- 8:41Speaker 2: Oh yeah, you can set it to rotate weekly, daily, yearly,
- 8:44Speaker 2: whatever you need.
- 8:46Speaker 1: Let's put these skills into practice with some real world scenarios.
- 8:49Speaker 1: Scenario one, cybersecurity. How can we use log analysis to
- 8:55Speaker 1: spot an intruder trying to brute force their way into SSH?
- 8:58Speaker 2: Okay, So we target the authentication log, which is varlogof
- 9:02Speaker 2: dot log. The trick isn't just finding failed logins, it's
- 9:05Speaker 2: finding a cluster of rapid failures, so too many, too
- 9:08Speaker 2: fast exactly. We're looking for multiple failed log ins for
- 9:11Speaker 2: the same valid user within a short window, like say
- 9:14Speaker 2: one hundred and twenty seconds. And the magic utility here
- 9:18Speaker 2: is actually the date command date white date. Because the
- 9:21Speaker 2: timestamps in the log are just strings, they're hard to
- 9:24Speaker 2: do math with, so we use GP and AWK to
- 9:27Speaker 2: filter out and extract the first and last failed log
- 9:30Speaker 2: in attempts for a specific IP. Then we piped those
- 9:33Speaker 2: timestamps into the date command to convert them into Unix
- 9:36Speaker 2: epoch seconds, which is just a big number. And once
- 9:39Speaker 2: you have two numbers, you can just subtract them to
- 9:41Speaker 2: find the difference. If it's less than one hundred and twenty,
- 9:43Speaker 2: you flag it clever.
- 9:45Speaker 1: Okay. Scenario two active user hours report. Maybe for billing
- 9:50Speaker 1: or just resource planning, you need to rank users by
- 9:53Speaker 1: their total time on the server.
- 9:54Speaker 2: For that, we use the last command. Again, it pulls
- 9:56Speaker 2: all that session data from varlog dotm. The duration is
- 10:00Speaker 2: in our dot minute dot second format, which again isn't
- 10:03Speaker 2: great for math, right, so use scripting to extract those durations,
- 10:06Speaker 2: convert everything into total seconds, sum them up for each
- 10:09Speaker 2: unique user, and then just sort the whole list. You
- 10:11Speaker 2: get a ranked list of users from most to least active.
- 10:14Speaker 1: And finally, what are a few essential tools for just
- 10:16Speaker 1: general system health and optimization.
- 10:18Speaker 2: Absolutely, if you're worried about power consumption, especially on laptops,
- 10:22Speaker 2: you should check out PowerTOP. It even has a tunables
- 10:25Speaker 2: tab with optimization suggestions. Nice for monitoring dist activity specifically,
- 10:31Speaker 2: there's iotop. You often run it with the iooption to
- 10:34Speaker 2: show only the processes that are actively doing IO. And finally,
- 10:38Speaker 2: if a filesystem is acting weird, you run fs check
- 10:41Speaker 2: or a sesscorric which can check for errors and even
- 10:45Speaker 2: try to fix them automatically with the sick flag.
- 10:47Speaker 1: You know what's really fascinating about all this is that
- 10:49Speaker 1: the power of a great system administrator doesn't come from
- 10:52Speaker 1: knowing a million obscure commands. It comes from knowing how
- 10:56Speaker 1: to chain like five core commands DU, sort, AWQP, and
- 11:01Speaker 1: head to perform this incredibly complex, almost forensic analysis.
- 11:05Speaker 2: You're right. These techniques turn that raw data into actual
- 11:09Speaker 2: knowledge that lets you debug, optimize, and secure your systems.
- 11:12Speaker 2: Your harness in the operating system's own intelligence to manage itself,
- 11:15Speaker 2: which is the whole point.
- 11:17Speaker 1: So here's a final provocative thought for you to explore.
- 11:19Speaker 1: After our deep dive. We discussed using rep and date
- 11:23Speaker 1: to detect intruders by checking the time difference between the
- 11:25Speaker 1: first and last failed log in attempts in a cluster. Now,
- 11:29Speaker 1: imagine an intruder tries to log in, fails, waits exactly
- 11:34Speaker 1: five minutes, and then tries again and fails again. How
- 11:37Speaker 1: might our simple script, which only checks the first and
- 11:40Speaker 1: last lines it finds, fail to accurately capture the total
- 11:43Speaker 1: duration of that intruder's activity. It might miss that five
- 11:46Speaker 1: minute gap entirely.
- 11:48Speaker 2: Ooh, that's a good one. It forces you to think
- 11:51Speaker 2: about the logic of how you process logs. You have
- 11:53Speaker 2: to consider the limitations of just grabbing chunks of data
- 11:56Speaker 2: versus iterating through every single entry to track continuity.
- 12:00Speaker 1: A perfect challenge to reinforce why robust scripting is so vital. Absolutely,
- 12:03Speaker 1: until next time, keep monitoring,