Wednesday, May 24, 2023

Preventing Email Floods in Linux: Understanding the Cause and Solution

Introduction

In a Linux system, it can be disconcerting to find a flood of emails in the /var/spool/mail/user_name directory, especially when no email was intentionally sent. This occurrence often indicates an underlying issue related to the redirection of output from scheduled scripts via crontab. This blog post will delve into the cause behind these email floods and provide a solution to prevent them from overwhelming your system.

Understanding the Scenario

The appearance of unexpected emails in /var/spool/mail/user_name typically arises when there is no proper redirection of script output within the scheduled tasks managed by crontab. The crontab utility allows users to schedule commands or scripts to run automatically at specified intervals. However, if the output and error streams of these scheduled scripts are not redirected appropriately, the default behavior is to send any output or error messages via email to the user executing the script.

Identifying the Cause

When a script scheduled via crontab produces output or encounters errors, Linux considers this important information and assumes the user wants to be notified. Consequently, without redirection, the system automatically sends emails to the user, which accumulates in the /var/spool/mail/user_name directory. This behavior aims to keep users informed about any potential issues or valuable information resulting from scheduled tasks.

Resolving the Issue

To prevent email floods and unnecessary clutter in the /var/spool/mail/user_name directory, it is crucial to redirect the output and error streams of scheduled scripts. By redirecting them to either be discarded or stored in a log file, users can effectively disable the crontab from sending email alerts.

Solution 1: Discarding Output and Errors

One simple approach is to redirect both output and errors to the null device (/dev/null). The null device acts as a black hole that discards any data written to it, effectively silencing the script and preventing email notifications.

To implement this redirection, modify the crontab entry for the script as follows:

script.sh > /dev/null 2>&1

Solution 2: Discarding Output Only

If you still want to receive error messages but wish to discard the regular output, you can redirect only the output stream to /dev/null while allowing error messages to be delivered via email. Use the following redirection:

script.sh > /dev/null

Solution 3: Redirecting to a Log File

If preserving the output and error messages for future reference is essential, you can redirect both streams to a designated log file. This enables you to review the script's output and errors at your convenience, without flooding your email inbox.

To redirect the output and errors to a log file, modify the crontab entry as shown:

script.sh > /path/to/directory/script.log 2>&1

Clearing/Purging Linux Mail

After implementing the appropriate redirection technique to prevent email floods, you might still encounter accumulated emails in the /var/spool/mail/user_name directory. To clear or purge these emails, follow these steps:

  1. Access the mail client by running the mail command in the terminal.
  2. Use the d * command to delete all emails in the mailbox.
  3. Confirm the deletion by typing q and pressing Enter.

Conclusion

Unwanted email floods in the /var/spool/mail/user_name directory can be a nuisance and indicate misconfigured output redirection in crontab. By understanding the cause behind this issue and implementing the appropriate solutions, you can prevent your Linux system from being overwhelmed by unnecessary email notifications. Whether you choose to discard output and errors, discard output only, or redirect them to a log file, taking proactive steps will ensure a smoother and more efficient system.

Monday, May 22, 2023

Certificate file formats - SSL/TLS & Keytool

Keytool has two main files as below

Keystore: Stores all keys and certificates

Truststore: Stores all trusted CA certificates


Types of Keystore:

i) JKS (Java keystore) -- holds Private keys & Certificates -- Default option

ii) JCEKS (Java Cryptography Extension keystore) -- holds Secret key, Private key & Certificates

iii) PKCS#12 -- Can be used by non java apps, Password based key data format 2 & uses DES-3(default) and AES encryption.

iv) PKCS#11 -- USB Token, Smartcard

v) Windows-My/Windows-Root

vi) Third party like Bouncy Castle, IAIK etc)



Example:

$ keytool -list -keystore keystore_node1.jks

Note: not mentioned store type as by default assumes jks

$ keytool -list -keystore keystore_node1.jceks -storetype jceks

$ keytool -list -keystore keystore_node1.pkcs12 -storetype pkcs12

##pkcs11 is basically a configuration file


Generating keystores:

$ keytool -genkey

Generates keystore in home directory(/home/bypramod/.keystore) of use using jks format by default.

$ keytool -list

Shows default from home directory

$ keytool -list -v

$ keytool -list -rfc

Will print certificate in PEM format

$ keytool -genkey -alias bypramod_node1

This avoids contradiction of not able to generate keystore multiples times as by default it use "mykey" name.

***

Sunday, May 21, 2023

Some useful Splunk queries for Datastax/Cassandra

1) Compaction error:

index="index_name" sourcetype="cassandra_cluster:system"  "Not enough space for compaction" 


2) DSE Process manual shutdown

index="index_name" "dse service shutdown" | table _time, host, _raw | rename _time as Time _raw as Event | convert ctime(Time) as Time 


3) Hints corrupted:

index="index_name" level=ERROR "*file is corrupted*" HintsDispatchExecutor | table _time host _raw 

4) Some Cassandra Exceptions:

index="index_name" cassandra_exception="InvalidRequestException" OR cassandra_exception="AlreadyExistsException" OR cassandra_exception="ConfigurationException" OR cassandra_exception="FunctionFailureException" OR cassandra_exception="WriteFailureException" OR cassandra_exception="ReadFailureException" OR cassandra_exception="CoordinationFailureException" OR cassandra_exception="RequestExecutionException" OR cassandra_exception="QueryValidationException" OR cassandra_exception="NoHostAvailableException" OR cassandra_exception="DriverException" OR cassandra_exception="UnavailableException" OR cassandra_exception="ReadTimeoutException" OR cassandra_exception="OperationTimedOutException" | eval suppress_field = host."-".cassandra_exception | table _time, host,suppress_field, _raw | rename _time as Time _raw as Event | convert ctime(Time) As Time 

5) OS Out of Memory killer invoked:

index="index_name" source="/var/log/messages" "oom-killer" OR "Out of memory: Kill process" OR "Killed process" | rex field=_raw "^(?:[^:\n]*:){3}\s+(?P<invoked_oom>\w+) invoked oom-killer" | rex field=_raw "^(?:[^ \n]* ){10}(?P<killed_pid>[^\(]+)\((?P<killed_process>\w+)" | transaction _time | table host invoked_oom killed_process _raw 


***

Friday, May 19, 2023

How to Fix the "Busy Initramfs Error on Ubuntu" in Ubuntu 22

Introduction
    Encountering errors during system startup can be frustrating, and one such error is the "Busy Initramfs Error" on Ubuntu. This error often occurs after a system restart and can leave users stranded at the busybox "initramfs" shell prompt. However, there is a straightforward solution to resolve this issue and get your Ubuntu system up and running smoothly again. In this article, we will walk you through the steps to fix the "Busy Initramfs Error" on Ubuntu 22. 

Step 1: Identifying the Affected Disk: When you encounter the "Busy Initramfs Error," the first thing you need to do is identify the disk volume causing the problem. In your case, the error was related to the disk volume /dev/sda7. Take note of the disk volume mentioned in the error message as it will be required in the subsequent steps. 

Step 2: Running File System Check (fsck): Once you have identified the affected disk volume, you can proceed with running a file system check (fsck) on that specific disk. This process will help identify and fix any errors or inconsistencies with the file system. To initiate the file system check, follow these steps: At the busybox "initramfs" shell prompt, enter the command:
(initramfs) fsck /dev/sda7 -y
    Replace "/dev/sda7" with the disk volume you identified earlier. The fsck command will start analyzing the file system and attempt to repair any issues it finds. Be patient as this process may take some time, depending on the size of your disk and the severity of the errors. 

Step 3: Rebooting the System: After the file system check has completed and any errors have been resolved, you can proceed to reboot your Ubuntu system. 

1)  At the busybox "initramfs" shell prompt, enter the command:
(initramfs) reboot
2) The system should initiate a reboot, allowing you to access Ubuntu normally. However, if the reboot command doesn't work, you can try typing "exit" and hitting ENTER. This will exit the busybox shell and might bring you back to the regular boot process. 

Conclusion: Encountering the "Busy Initramfs Error" on Ubuntu can be troublesome, but it doesn't have to be a major roadblock. By following the steps outlined in this article, you can effectively resolve this issue and get your Ubuntu 22 system back up and running smoothly. Remember to identify the affected disk volume, run a file system check (fsck), and reboot the system. With these steps, you should be able to overcome the "Busy Initramfs Error" and regain access to your Ubuntu system without any further complications.

***