Skip to content

GitEric77/FileCountMonitor

Repository files navigation

File Count Monitor

A Python-based AWS Lambda function that monitors file counts in directories (particularly EFS mounts) and publishes metrics to Amazon CloudWatch for monitoring and alerting.

Overview

This project provides a Lambda function that counts files in a specified directory and sends the count as a custom metric to CloudWatch. It's particularly useful for monitoring inode usage on Amazon EFS (Elastic File System) mounts.

Features

  • Count files in specified directories
  • Publish custom metrics to CloudWatch
  • Support for EFS mount monitoring
  • Configurable metric dimensions
  • Lambda-ready deployment

Files

  • FilecountvFinal.py - Main production Lambda function
  • filecount.py - Simple file counting utility
  • testapp.py / testapp2.py - Test applications for CloudWatch metrics
  • Filecountv1.py to Filecountv4.py - Development versions

Usage

Lambda Function

The main function (FilecountvFinal.py) monitors the /mnt/global directory and publishes metrics with the following dimensions:

  • Directory: PE1/Global
  • APP_VERSION: 1.0
  • Namespace: SAP
def lambda_handler(event, context):
    cloudwatch = boto3.client('cloudwatch')
    dirListing = os.listdir('/mnt/global')
    filecount = len(dirListing)
    
    # Publish to CloudWatch
    response = cloudwatch.put_metric_data(
        MetricData=[{
            'MetricName': 'Files',
            'Dimensions': [
                {'Name': 'Directory', 'Value': 'PE1/Global'},
                {'Name': 'APP_VERSION', 'Value': '1.0'}
            ],
            'Unit': 'None',
            'Value': filecount
        }],
        Namespace='SAP'
    )

Prerequisites

  • AWS Lambda environment
  • Python 3.x
  • boto3 library
  • Appropriate IAM permissions for CloudWatch

IAM Permissions

The Lambda function requires the following CloudWatch permissions:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudwatch:PutMetricData"
            ],
            "Resource": "*"
        }
    ]
}

Deployment

  1. Package the Lambda function
  2. Deploy to AWS Lambda
  3. Configure appropriate IAM role
  4. Set up CloudWatch alarms based on the metrics

Monitoring

The function publishes metrics to CloudWatch under the SAP namespace with the metric name Files. You can create alarms and dashboards based on these metrics to monitor file count thresholds.

License

This project is provided as-is for educational and monitoring purposes.

About

Monitor file counts and publish to CloudWatch

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages