Applications Systems
How to Monitor File Size on Linux Servers Using Bash in SolarWinds SAM
Monitor file size on Linux servers using a Bash script in SolarWinds SAM by leveraging the Linux/Unix Script Monitor component. The script outputs file size in MB and follows the required format for SAM parsing using Statistic and Message identifiers.
First published date
Last published date
Overview
Product section
Resolution
Access SolarWinds SAM
- Log in to the SolarWinds Platform Web Console.
- Navigate to:
Settings → All Settings → SAM Settings → Component Monitors → Create New Component Monitor.
Select Component Type
- Choose Linux/Unix Script Monitor.
- Click Continue.
Define Credentials
- In Credential for Monitoring, select:
- Inherit from Node (if node credentials are already configured), or
- Specify a Linux account with SSH access and permission to read the target file.
Set Script Working Directory
- Enter:
/tmp - This is the recommended directory for temporary script execution.
Modify Command Line
- Enter:
/bin/bash ${SCRIPT} - This ensures the uploaded script runs using Bash.
-
Add Script Body
-- Scripts are not supported under any SolarWinds support program or service.
-- Scripts are provided AS IS without warranty of any kind. SolarWinds further
-- disclaims all warranties including, without limitation, any implied warranties
-- of merchantability or of fitness for a particular purpose. The risk arising
-- out of the use or performance of the scripts and documentation stays with you.
-- In no event shall SolarWinds or anyone else involved in the creation,
-- production, or delivery of the scripts be liable for any damages whatsoever
-- (including, without limitation, damages for loss of business profits, business
-- interruption, loss of business information, or other pecuniary loss) arising
-- out of the use of or inability to use the scripts or documentation.- Paste the following script:
#!/bin/bash
# Specify the file path here
FILE="/opt/file-size-test/messages"if [ -f "$FILE" ]; then
# Get exact bytes (GNU stat on Linux)
bytes=$(stat -c%s "$FILE")
# Convert to MB with 2 decimal places (pure numeric)
size_mb=$(awk -v b="$bytes" 'BEGIN {printf "%.2f", b/1024/1024}')
# Emit SAM-friendly fields (colon format, no spaces)
echo "Statistic.FileSizeMB:${size_mb}"
echo "Message.FileSize:File '${FILE}' size is ${size_mb} MB"
exit 0
else
echo "Statistic.FileSizeMB:0"
echo "Message.FileSize:File '${FILE}' not found"
exit 2
fiTest the Script
- Click Get Script Output.
- Verify the output:
Statistic.FileSizeMB:5.34 Message.FileSize:File '/opt/file-size-test/messages' size is 5.34 MB - Ensure Output Result shows fields recognized.
Save and Assign
- Save the component monitor.
- Assign it to the desired Linux node or application template.
Configure Thresholds
- Set thresholds for
FileSizeMBto trigger alerts when the file exceeds defined limits.