# Terminal Session Monitoring Script
This script monitors active terminal sessions on specified servers, tracks session durations, and stores the data in a SQLite database. It is designed to be easily customizable for various use cases, such as generating reports, exporting data to external systems, or integrating with APIs.
## Features
- **Active Session Monitoring**: Tracks active terminal sessions on multiple servers.
- **Session Duration Tracking**: Calculates the duration of each session and updates the total time for each user.
- **Database Storage**: Stores session data in a SQLite database for easy querying and analysis.
- **Customizable Export**: Provides examples for exporting data to CSV, external APIs, or other formats.
## Requirements
- Python 3.6 or higher
- SQLite3 (included with Python)
- `subprocess` module (included with Python)
- `logging` module (included with Python)
## Installation
1. Clone the repository:
```bash
git clone https://github.com/yourusername/terminal-monitoring.git
cd terminal-monitoring
-
Install dependencies (if any):
pip install -r requirements.txt
-
Configure the script:
- Update the
SERVERS
list in the script with your server names. - Adjust the
CHECK_INTERVAL
(in seconds) to control how often the script checks for active sessions.
- Update the
Run the script:
python terminal_monitoring.py
The script will:
- Monitor active sessions on the specified servers.
- Store session data in a SQLite database (
sessions.db
). - Log activities to
script.log
.
You can customize the script to export data in various formats. Here are some examples:
To export user session data to a CSV file, add the following code to the main
function:
with open("report.csv", "w") as file:
file.write("Username,TotalMinutes\n")
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
cursor.execute("SELECT Username, TotalMinutes FROM Users;")
for row in cursor.fetchall():
file.write(f"{row[0]},{row[1]}\n")
To send data to an external API, use the requests
library:
import requests
def export_to_api(data):
url = "https://yourapi.com/endpoint"
response = requests.post(url, json=data)
if response.status_code == 200:
logging.info("Data successfully exported to the API.")
else:
logging.error(f"Failed to export data to the API: {response.status_code}")
# Example usage in the main function:
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
cursor.execute("SELECT Username, TotalMinutes FROM Users;")
data = [{"username": row[0], "total_minutes": row[1]} for row in cursor.fetchall()]
export_to_api(data)
To generate a PDF report, use a library like ReportLab
:
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
def generate_pdf_report():
pdf = canvas.Canvas("report.pdf", pagesize=letter)
pdf.drawString(100, 750, "User Session Report")
y = 700
with sqlite3.connect(DB_PATH) as conn:
cursor = conn.cursor()
cursor.execute("SELECT Username, TotalMinutes FROM Users;")
for row in cursor.fetchall():
pdf.drawString(100, y, f"User: {row[0]}, Total Minutes: {row[1]}")
y -= 20
pdf.save()
# Example usage in the main function:
generate_pdf_report()
This project is licensed under the MIT License. See the LICENSE file for details.