Blog / Linux/ Lightweight Server Monitoring with Lighttpd: Exposing Connection Counts via JSON API

Lightweight Server Monitoring with Lighttpd: Exposing Connection Counts via JSON API

简单API方式实现监控lighttpd服务器当前IP数等,并以json数据返回给PHP调用

Background and Approach

For a website using Lighttpd as an attachment server, we needed to implement client-side traffic distribution based on the server's current connection count.

Initially, SNMP was considered, but frequent timeout issues led us to a simpler alternative: create a lightweight API that returns JSON data for client consumption.

The overall solution is as follows:

  1. Configure a Lighttpd virtual host on a specific port to serve a JSON file.
  2. Write a shell script, scheduled via cron (e.g., every 5 minutes), to collect server TCP connection information.
  3. The script writes the collected data to a JSON file in the virtual host directory.
  4. Clients request this JSON file to obtain data for traffic distribution decisions.

Configuring the Lighttpd Virtual Host

Create the Directory

First, create a directory to hold the JSON file.

mkdir -p /srv/www/vhosts/ipcount

Modify the Main Configuration

Back up your configuration file before editing.

cd /etc/lighttpd/
cp -a lighttpd.conf lighttpd.conf.backup
vi /etc/lighttpd/lighttpd.conf

Add the following configuration at the end of the file:

$SERVER["socket"] == "static.xxx.com:666" {
    server.document-root = "/srv/www/vhosts/ipcount/"
}

This directs Lighttpd to use the specified directory as the document root for requests on port 666.

Verify Configuration and Restart Service

Important: Always verify the configuration syntax before restarting to avoid service disruption.

lighttpd -t -f /etc/lighttpd/lighttpd.conf
systemctl restart lighttpd

Configure Firewall

If a firewall is active, open the required port.

firewall-cmd --permanent --add-port=666/tcp
firewall-cmd --reload

Test the Virtual Host

Create a test file to verify the setup.

echo "Test Page" > /srv/www/vhosts/ipcount/index.html

Access http://static.xxx.com:666/index.html in a browser. You should see "Test Page".

Writing the Data Collection Shell Script

Create a shell script to collect server metrics and generate the JSON file.

#!/bin/bash
# Script: ServerIPcount.sh
# Purpose: Collect established connection count and bandwidth.

# 1. Count ESTABLISHED connections on port 80
establishedIP=$(netstat -na | grep -i ":80" | grep ESTABLISHED | wc -l)

# 2. Calculate eth0 egress bandwidth (bytes/sec) over 1 second
TXpre=$(cat /proc/net/dev | grep "eth0" | tr : " " | awk '{print $10}')
sleep 1
TXnext=$(cat /proc/net/dev | grep "eth0" | tr : " " | awk '{print $10}')
bandwidth=$((TXnext - TXpre))

# 3. Write data to JSON file
JSON_DATA='{"establishedIP":"'$establishedIP'","bandwidth":"'$bandwidth'"}'
echo $JSON_DATA > /srv/www/vhosts/ipcount/index.json

Save the script (e.g., to /tools/ServerIPcount.sh) and make it executable:

chmod +x /tools/ServerIPcount.sh

Configure the Cron Job

Schedule the script to run every 5 minutes.

crontab -e

Add this line:

*/5 * * * * /tools/ServerIPcount.sh

The script will generate a JSON file like:

{"establishedIP":"42","bandwidth":"1250000"}

PHP Client Example

A PHP client can fetch and process the JSON data.

<?php
$json_url = 'http://static.xxx.com:666/index.json';
$json_string = file_get_contents($json_url);

if ($json_string === false) {
    die('Failed to fetch server status.');
}

$data = json_decode($json_string, true);

if (json_last_error() !== JSON_ERROR_NONE) {
    die('JSON parse error: ' . json_last_error_msg());
}

// Example traffic distribution logic
$establishedIP = $data['establishedIP'];
if ($establishedIP > 100) {
    $download_server = 'backup-server.example.com';
} else {
    $download_server = 'main-server.example.com';
}
echo "Current download server: " . $download_server;
?>

Advanced: Combining with Time-Based Logic

You can combine connection counts with time-of-day checks for more sophisticated rules.

<?php
function is_within_time_section($start_hour = 9, $end_hour = 18) {
    $current_hour = (int)date('H');
    return ($current_hour >= $start_hour && $current_hour < $end_hour);
}

// Usage example
if (is_within_time_section()) {
    echo "Peak hours (9:00-18:00). Apply stricter rules.";
} else {
    echo "Off-peak hours.";
}
?>

Summary

This article presents a lightweight server monitoring and traffic distribution solution. By serving a static JSON file via Lighttpd, updated by a scheduled shell script, clients can easily obtain real-time connection counts and bandwidth data. This approach avoids SNMP complexity and timeouts, is simple to implement, and integrates seamlessly with PHP or other client applications for intelligent, load-based traffic routing.

Post a Comment

Your email will not be published. Required fields are marked with *.