# Monitoring

### Understanding your system

So now we want to be able to navigate, operate and monitor our system. To do this, most servers have a relatively homogenous system: systemd!

systemd is a software suite that provides an array of system components for Linux operating systems. The main aim is to unify service configuration and behavior across Linux distributions.

`systemctl` is the command-line tool that manages the systemd system and service manager in Linux.

* How to work with systemd units, etc
  * Many modern Linux distros go with systemd — it can handle services for you in a convenient manner
  * It comes with systemctl, along with a lot of other things — DNS caching resolvers, time sync, a bootloader, etc, But we'll want to focus on systemctl
  * Start/stop/restart services: systemctl start unit.service, etc
  * Check service status: systemctl status unit.service

`journalctl` is a utility for querying and displaying logs from journald, systemd’s logging service.

To see live logs:

```bash
journalctl -f
```

To see the first 20 lines

```bash
journalctl -n 20
```

To check a specific service:

```bash
journalctl -u sshd
```

To get all logs from last boot

```bash
journalctl -b
```

To filter by time (last 15 minutes for example)

```bash
journalctl --since "15 minutes ago"
```

### Monitoring with top

Aside from reading warnings, and making sure our services are running, we often times also want to make sure our system as a whole is running as expected. This is where `top` and `htop` come into play

#### What is top?

* top command is used to show the Linux processes. It provides a dynamic real-time view of the running system.
* Think of it as a super powerful task manager for Linux.

#### Basic Usage

* Just type `top` to start the program.
* Pressing q will simply exit the command mode.
* Pressing h will show you the help menu.

#### What does everything mean?

* PID: Shows task’s unique process id.
* PR: The process’s priority. The lower the number, the higher the priority.
* VIRT: Total virtual memory used by the task.
* USER: User name of owner of task.
* %CPU: Represents the CPU usage.
* TIME+: CPU Time, the same as ‘TIME’, but reflecting more granularity through hundredths of a second.
* SHR: Represents the Shared Memory size (kb) used by a task.
* NI: Represents a Nice Value of task. A Negative nice value implies higher priority, and positive Nice value means lower priority.
* %MEM: Shows the Memory usage of task.
* RES: How much physical RAM the process is using, measured in kilobytes.
* COMMAND: The name of the command that started the process.

#### `top` cheatsheet

{% embed url="<https://gist.github.com/ericandrewlewis/4983670c508b2f6b181703df43438c3>" %}

#### Some exercises!

* Try running `top` and see what you can find out about your system!
* What are the top 5 processes using the most CPU?
* What would I press if I want to kill the processes using the most memory?
* I want to see what processes start running when I start my computer. How would I do that?

<details>

<summary>Solution</summary>

* What are the top 5 processes using the most CPU?
  * `top` -> `Shift + P`
* What would I press if I want to kill the processes using the most memory?
  * `top -> Shift + M` -> `k`
* I want to see what processes start running when I start my computer.
  * `top` -> `f` -> `PID` -> `s` -> `q` -> `Shift + R`

</details>
