Problem overview
Ensuring critical log messages or specific strings are present in key Linux files can be tedious without an automated check. This script helps IT Professionals and MSPs proactively detect missing entries that might indicate configuration errors, security issues, or incomplete processes, reducing manual oversight and speeding up resolution times.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Level Library
# level.io/library/script-linux-monitor-file-contains
# Specify the file path to check
file_path="{{FilePath}}"
# Specify the string to search for in the file
search_string="{{SearchString}}"
# -----------------------------------------------------------------------------
# Check if the file exists
if [ -f "$file_path" ]; then
if grep -q "$search_string" "$file_path"; then
echo "SUCCESS: The string '$search_string' exists in the file."
else
echo "ERROR: The string '$search_string' does not exist in the file."
fi
else
echo "ERROR: File not found: $file_path"
fi This script checks whether a designated string exists within a specified file. It uses Level Script Variables for both the file path and search term, providing flexibility in various environments. If the file is found and the string exists, it returns a success message; otherwise, it reports an error.
You can easily integrate this script into a script-based monitor in Level to receive alerts whenever the string is missing. By pairing it with an automated remediation workflow, you can immediately address issues detected in your environment.
Use cases
- Monitoring log files for critical error patterns
- Verifying configuration files for essential directives
- Ensuring required security settings are present in system files
- Automating checks for newly added or changed file contents
Recommendations
- Test the script in a non-production environment before widespread deployment
- Use a script-based monitor in Level to trigger this script and alert if the string is missing
- Consider building an automation in Level to run this script on a schedule if continuous checks are needed
- Make sure the file path and string variables are accurate and up to date
- Keep an eye on script output for “ERROR” vs “SUCCESS” messages to pinpoint issues quickly