#!/usr/bin/env zx import os from 'os'; import dotenv from 'dotenv'; import fs from 'fs'; import sgMail from '@sendgrid/mail'; // Load environment variables from .env file dotenv.config(); // Email configuration from environment variables const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY; const EMAIL_FROM = process.env.EMAIL_FROM; const EMAIL_TO = process.env.EMAIL_TO; const EMAIL_SUBJECT = 'ClamAV Virus Scan Alert'; // Configure SendGrid sgMail.setApiKey(SENDGRID_API_KEY); // Logger function function log(message) { fs.appendFileSync(`/var/log/clamav/detected.log`, `${new Date().toISOString()} - ${message}\n`); } // Function to send email async function sendEmail(subject, body) { const msg = { to: EMAIL_TO, from: EMAIL_FROM, subject: subject, html: body, }; try { await sgMail.send(msg); log('Email sent successfully'); } catch (error) { log(`Failed to send email: ${error.message}`); throw new Error('Failed to send email'); } } (async () => { try { // Extract scan details from environment variables const filePath = process.env.CLAMAV_FILE_PATH || 'Unknown file'; const virusName = process.env.CLAMAV_VIRUS_NAME || 'Unknown virus'; const scanDate = new Date().toISOString(); const hostname = os.hostname(); const username = process.env.USER || process.env.USERNAME || 'Unknown user'; // Create email body const emailBody = `

ClamAV Virus Scan Alert

Date: ${scanDate}

File Path: ${filePath}

Virus Name: ${virusName}

Hostname: ${hostname}

User: ${username}

`; // Send email await sendEmail(EMAIL_SUBJECT, emailBody); } catch (error) { log(`Script execution failed: ${error.message}`); } })();