Get Notified When Your Computer State Changes

October 19, 2024

Beyond monitoring the internet, you can use Tasking.agency agents to monitor your local computer and notify you when conditions change. This tutorial shows you how to create validation scripts that check system state and trigger different agent tasks based on the results.

For example, you might want one notification when disk space is running low, and a different notification when everything is healthy. Or you might want to be alerted when a critical service stops running.

The Basic Pattern

The key pattern is: validate a condition, then trigger different agents based on whether the condition passes or fails. Here's a disk space monitoring example for each platform:

bash
#!/bin/bash
# monitor-disk-space.sh

AGENT_ID="your-agent-id-here"
API_URL="http://0.0.0.0:3021/api/agents/${AGENT_ID}/chat"
THRESHOLD=90

# Get disk usage percentage
USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')

if [ "$USAGE" -gt "$THRESHOLD" ]; then
  MESSAGE="URGENT: Disk space is at ${USAGE}%. Please clean up files immediately."
else
  MESSAGE="Disk space is healthy at ${USAGE}%."
fi

curl -X POST "${API_URL}" \
  -H 'Content-Type: application/json' \
  -d "{
    \"message\": \"${MESSAGE}\"
  }"
Monitor disk space and send conditional alerts

Monitoring Service Status

You can monitor whether critical services are running and get notified when they stop:

bash
#!/bin/bash
# monitor-service.sh

SERVICE_NAME="postgresql"
AGENT_ID="your-agent-id-here"
API_URL="http://0.0.0.0:3021/api/agents/${AGENT_ID}/chat"

# Check if service is running (macOS)
if launchctl list | grep -q "${SERVICE_NAME}"; then
  STATUS="running"
  MESSAGE="Service ${SERVICE_NAME} is running normally."
else
  STATUS="stopped"
  MESSAGE="ALERT: Service ${SERVICE_NAME} has stopped! Please investigate immediately."
fi

curl -X POST "${API_URL}" \
  -H 'Content-Type: application/json' \
  -d "{
    \"message\": \"${MESSAGE}\"
  }"
Monitor service status and send alerts when services stop

Watching for File Changes

You can monitor files or directories for changes and trigger agents when modifications occur:

bash
#!/bin/bash
# monitor-file-changes.sh

WATCH_FILE="/path/to/important/config.json"
CHECKSUM_FILE="/tmp/config-checksum.txt"
AGENT_ID="your-agent-id-here"
API_URL="http://0.0.0.0:3021/api/agents/${AGENT_ID}/chat"

# Calculate current checksum (use md5 on macOS)
CURRENT_CHECKSUM=$(md5 -q "${WATCH_FILE}")

# Check if we have a previous checksum
if [ -f "${CHECKSUM_FILE}" ]; then
  PREVIOUS_CHECKSUM=$(cat "${CHECKSUM_FILE}")

  if [ "${CURRENT_CHECKSUM}" != "${PREVIOUS_CHECKSUM}" ]; then
    # File has changed
    MESSAGE="ALERT: Configuration file has been modified! Previous checksum: ${PREVIOUS_CHECKSUM}, New checksum: ${CURRENT_CHECKSUM}"

    curl -X POST "${API_URL}" \
      -H 'Content-Type: application/json' \
      -d "{
        \"message\": \"${MESSAGE}\"
      }"
  fi
fi

# Save current checksum
echo "${CURRENT_CHECKSUM}" > "${CHECKSUM_FILE}"
Detect file changes and send notifications

Using Multiple Agents

For more sophisticated monitoring, you can route different alerts to different agents:

bash
#!/bin/bash
# advanced-monitoring.sh

ALERT_AGENT_ID="critical-alerts-agent"
STATUS_AGENT_ID="status-updates-agent"
ALERT_API="http://0.0.0.0:3021/api/agents/${ALERT_AGENT_ID}/chat"
STATUS_API="http://0.0.0.0:3021/api/agents/${STATUS_AGENT_ID}/chat"

# Run your validation check
DISK_USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')

if [ "$DISK_USAGE" -gt 90 ]; then
  # Critical - send to alert agent
  curl -X POST "${ALERT_API}" \
    -H 'Content-Type: application/json' \
    -d "{
      \"message\": \"CRITICAL: Disk at ${DISK_USAGE}%! Send urgent notification.\"
    }"
else
  # Normal - send to status agent
  curl -X POST "${STATUS_API}" \
    -H 'Content-Type: application/json' \
    -d "{
      \"message\": \"Disk usage is normal at ${DISK_USAGE}%.\"
    }"
fi
Route alerts to different specialized agents

Best Practices

When setting up system monitoring with agents:

• Use appropriate check intervals - don't check too frequently

• Implement rate limiting to avoid flooding your agents

• Store state (like checksums) to detect changes over time

• Use descriptive task IDs to track monitoring history

• Consider using different agents for alerts vs. status updates

• Test your scripts manually before scheduling them