All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aoindustries.aoserv.daemon.monitor.NetStatMonitor.txt Maven / Gradle / Ivy

/*
 * aoserv-daemon - Server management daemon for the AOServ Platform.
 * Copyright (C) 2000-2009, 2018, 2019, 2021, 2022  AO Industries, Inc.
 *     [email protected]
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of aoserv-daemon.
 *
 * aoserv-daemon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aoserv-daemon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with aoserv-daemon.  If not, see .
 */
package com.aoindustries.aoserv.daemon.monitor;

import com.aoindustries.aoserv.client.net.Bind;
import com.aoindustries.aoserv.client.NetProtocol;
import com.aoindustries.aoserv.daemon.AOServDaemon;
import com.aoindustries.aoserv.daemon.AOServDaemonConfiguration;
import com.aoapps.collections.SortedArrayList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO: Move this to NOC
 *
 * The configuration for all AOServ processes is stored in a properties file.
 *
 * @author  AO Industries, Inc.
 */
final public class NetStatMonitor implements Runnable {

  private static final String[] netstat={"/bin/netstat", "-ln"};

  /**
   * The minimum delay between scans.
   */
  public static final int MIN_DELAY=5*60*1000;

  /**
   * The minimum delay after an error occurs.
   */
  public static final int MIN_ERROR_DELAY=15*60*1000;

  /**
   * The maximum delay between scans.
   */
  public static final int MAX_DELAY=15*60*1000;

  private static Thread thread;

  private NetStatMonitor() {
  }

  private static int getRandomDelay() {
    return MIN_DELAY+AOServDaemon.getFastRandom().nextInt(MAX_DELAY-MIN_DELAY);
  }

  public void run() {
    while (!Thread.currentThread().isInterrupted()) {
      try {
        while (!Thread.currentThread().isInterrupted()) {
          try {
            Thread.sleep(getRandomDelay());
          } catch (InterruptedException err) {
            LogFactory.getLogger(this.getClass()).log(Level.WARNING, null, err);
            // Restore the interrupted status
            Thread.currentThread().interrupt();
            break;
          }

          // Get the current list of binds for this server
          List nbs=AOServDaemon.getThisAOServer().getServer().getNetBinds();

          // Build a ArrayList that will be trimmed as matches are found
          List dbBinds=new ArrayList(nbs.size());
          for (int c=0;c extraBinds=new ArrayList();

          // Parse through each net bind returned by netstat
          Process P=Runtime.getRuntime().exec(netstat);
          try {
            P.getOutputStream().close();
            BufferedReader in=new BufferedReader(new InputStreamReader(P.getInputStream()));
            try {
              List allNetStats=new SortedArrayList();
              String line;
              while ((line=in.readLine())!=null) {
                NetStat netStat=NetStat.parseNetStat(line);
                if (netStat!=null) {
                  // Skip port 1 raw
                  if (netStat.port!=1 || !netStat.net_protocol.equals(NetProtocol.RAW)) {
                    // Must be unique to continue
                    if (!allNetStats.contains(netStat)) {
                      allNetStats.add(netStat);
                      // Find one or more matches and remove, otherwise add to extra bind info
                      boolean found=false;
                      for (int c=0;c




© 2015 - 2025 Weber Informatics LLC | Privacy Policy