4.3 Lesson 2 - Where Data Is Stored
4.3 Lesson 2 - Where Data Is Stored
4.3 Lesson 2
●
You can get a dynamic listing of all running processes with the top command:
●
●
The displayed information has to do with the process identifier (PID), the terminal in which the
●
process is run (TTY), the CPU time taken by the process (TIME) and the command which started the
●
process (CMD).
●
In combination with other switches, -f shows the relationship between parent and child processes:
●
●
We have already seen the /proc filesystem. /proc includes a numbered subdirectory for everyrunning process
in the system (the number is the PID of the process):
●
●
Thus, all the information about a particular process is included within its directory.
●
Each process on a system can potentially consume system resources. The so-called system load tries to aggregate
the overall load of the system into a single numeric indicator. You can see the current load with the command uptime:
●
●
The three last digits indicate the system’s load average for the last minute (2.91), the last five
●
minutes (1.59) and the last fifteen minutes (0.39), respectively.
●
Each of these numbers indicates how many processes were waiting either for CPU resources or
●
for input/output operations to complete. This means that these processes were ready to run if they
●
had received the respective resources.
●
As soon as the kernel and the processes start executing and communicating with each other,
a lot
●
of information is produced. Most of it is sent to files — the so-called log files or, simply, logs.
●
Without logging, searching for an event that happened on a server would give sysadmins
many a
●
headache, hence the importance of having a standardized and centralized way of keeping
track of
●
any system events. Besides, logs are determinant and telling when it comes to
troubleshooting and
●
security as well as reliable data sources for understanding system statistics and making trend
●
predictions.
●
Logging with the syslog Daemon
●
Traditionally, system messages have been managed by the standard logging facility — syslog — or any of its
derivatives — syslog-ng or rsyslog. The logging daemon collects messages from other services and programs and
stores them in log files, typically under /var/log. However, somservices take care of their own logs (take — for example
— the Apache HTTPD web server).
●
Likewise, the Linux kernel uses an in-memory ring buffer for storing its log messages.
●
Log Files in /var/log
●
Because logs are data that varies over time, they are normally found in /var/log.If you explore /var/log, you
will realize that the names of logs are — to a certain degree — quite self-explanatory. Some examples
include:
●
/var/log/auth.log
●
It stores information about authentication.
●
/var/log/kern.log
●
It stores kernel information.
●
/var/log/syslog
●
It stores system information.
●
/var/log/messages
●
It stores system and application data.
●
●
NOTE
●
The exact name and contents of log files may vary across Linux distributions.
●
Accessing Log Files
●
When exploring log files, remember to be root (if you do not have reading permissions) and use a pager such
as less;
●
●
Log Rotation
●
Log files can grow a lot over a few weeks or months and take up all free disk space. To tackle this, the utility
logrotate is used. It implements log rotation or cycling which implies actions such as moving log files to a new
name, archiving and/or compressing them, sometimes emailing them to the sysadmin and eventually deleting
them as they grow old.
●
The conventions used for naming these rotated log files are diverse (adding a suffix with the date, for
example); however, simply adding a suffix with an integer is commonplace:
●
●
The kernel ring buffer is a fixed-size data structure that records kernel boot messages as well as any live
kernel messages. The function of this buffer — a very important one — is that of logging all the kernel
messages produced on boot — when syslog is not yet available.
●
The dmesg command prints the kernel ring buffer (which used to be also stored in /var/log/dmesg). Because
of the extension of the ring buffer, this command is normally used in combination with the text filtering utility
grep or a pager such as less. For instance, to search for boot messages:
●
●
As of 2015, systemd replaced SysV Init as a de facto system and service manager in most major Linux distributions. As a
consequence, the journal daemon — journald — has become the standard logging component, superseding syslog in most
aspects. The data is no longer stored in plain text but in binary form.
●
Thus, the journalctl utility is necessary to read the logs. On top of that, journald is syslog compatible and can be integrated with
syslog. journalctl is the utility that is used to read and query systemd’s journal database. If invoked without options, it prints the
entire journal:
●
In the context of data storage, the following topics have been discussed in this lesson: process
●
management and system logging and messaging.
●
Regarding process management, we have learned the following:
●
• Programs generate processes and processes exist in a hierarchy.
●
• Every process has a unique identifier (PID) and a parent process identifier (PPID).
●
• top is a very useful command to dynamically and interactively explore the running processes
●
of the system.
●
• ps can be used to obtain a snapshot of the current running processes in the system.
●
• The /proc directory includes directories for every running process in the system named after
●
their PIDs.
●
• The concept of system
●
utilization/overloading.
●
Concerning system logging, we must remember that:
●
• A log is a file where system events are recorded. Logs are invaluable when it comes to
●
troubleshooting.
●
• Logging has traditionally been handled by special services such as syslog, syslog-ng or rsyslog.
●
Nevertheless, some programs use their own logging daemons.
●
• Because logs are variable data, they are kept in /var and — sometimes — their names can give
●
you a clue about their content (kern.log, auth.log, etc.)
●
• Most logs are written in plain text and can be read with any text editor as long as you have the
●
right permissions. However, a few of them are binary and must be read using special
●
commands.
●
• To avoid problems with disk space, log rotation is carried out by the logrotate utility.
●
• As for the kernel, it uses a circular data structure — the ring buffer — where boot messages are
●
kept (old messages fade away over time).
●
• The system and service manager systemd replaced System V init in virtually all distros with
●
journald becoming the standard logging service.
●
• To read systemd’s journal, the journalctl utility is needed.
●
Commands used in this lesson:
●
Cat; Concatenate/print file content.
●
Dmesg; Print the kernel ring buffer.
●
Echo; Display a line of text or a newline.
●
File; Determine file type.
●
Grep; Print lines matching a pattern.
●
Last; Show a listing of last logged in users.
●
Less; Display contents of file one page at a time.
●
Ls; List directory contents.
●
Journalctl ;Query the systemd journal.
●
Tail; Display the last lines of a file.