ln.skyscanner/crawl/Crawler.cs

465 lines
15 KiB
C#
Raw Normal View History

2019-03-13 08:20:53 +01:00
// /**
// * File: Crawler.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
2019-03-13 14:18:05 +01:00
using ln.types.threads;
2019-03-13 08:20:53 +01:00
using System.Net;
using System.Collections.Generic;
using System.IO;
using ln.logging;
using ln.types;
using System.Linq;
using ln.types.serialize;
using ln.skyscanner.entities;
using System.Net.NetworkInformation;
using ln.snmp;
using ln.snmp.endpoint;
using ln.snmp.rfc1213;
using ln.perfdb;
using ln.perfdb.storage;
using ln.skyscanner.check;
using System.Threading;
2019-03-15 07:43:12 +01:00
using ln.snmp.types;
2019-03-13 08:20:53 +01:00
namespace ln.skyscanner.crawl
{
2019-03-15 07:43:12 +01:00
public class Crawler
2019-03-13 08:20:53 +01:00
{
2019-03-14 13:31:15 +01:00
public SkyScanner SkyScanner { get; }
2019-03-13 08:20:53 +01:00
public String BasePath { get; set; }
public String PerfPath => Path.Combine(BasePath, "perfdb");
bool stopping;
2019-03-15 15:35:44 +01:00
Pool crawlThreadPool = new Pool(12);
2019-03-13 08:20:53 +01:00
2019-03-15 15:35:44 +01:00
public PoolJob[] CurrentJobs => crawlThreadPool.CurrentPoolJobs;
public PoolJob[] QueuedJobs => crawlThreadPool.QueuedJobs;
2019-03-13 08:20:53 +01:00
public DiskObject<CrawlPool> _CrawlPool;
public CrawlPool CrawlPool => _CrawlPool.Instance;
2019-03-15 15:35:44 +01:00
public SNMPEngine SNMPEngine { get; private set; }
2019-03-13 08:20:53 +01:00
2019-03-15 07:43:12 +01:00
Thread threadScheduler;
2019-03-13 08:20:53 +01:00
2019-03-14 13:31:15 +01:00
public Crawler(SkyScanner skyScanner)
2019-03-13 08:20:53 +01:00
{
2019-03-14 13:31:15 +01:00
SkyScanner = skyScanner;
2019-03-15 15:35:44 +01:00
try
{
BasePath = Path.Combine(skyScanner.BasePath, "crawler");
2019-03-14 13:31:15 +01:00
2019-03-15 15:35:44 +01:00
if (!Directory.Exists(BasePath))
Directory.CreateDirectory(BasePath);
if (!Directory.Exists(PerfPath))
Directory.CreateDirectory(PerfPath);
2019-03-13 08:20:53 +01:00
2019-03-15 15:35:44 +01:00
SNMPEngine = new SNMPEngine();
SNMPEngine.Timeout = 1250;
2019-03-13 08:20:53 +01:00
2019-03-15 15:35:44 +01:00
_CrawlPool = new DiskObject<CrawlPool>(String.Format("{0}/pool", BasePath));
2019-03-13 08:20:53 +01:00
2019-03-15 15:35:44 +01:00
threadScheduler = new Thread(scheduler);
threadScheduler.Start();
} catch (Exception)
{
Stop();
2019-03-13 08:20:53 +01:00
2019-03-15 15:35:44 +01:00
throw;
}
}
public void Start()
{
if (CrawlerState == ComponentState.INITIALIZED)
{
stopping = false;
SNMPEngine = new SNMPEngine();
}
2019-03-13 08:20:53 +01:00
}
public void Stop()
{
stopping = true;
crawlThreadPool.Close();
SNMPEngine.Close();
_CrawlPool.Save();
}
2019-03-15 15:35:44 +01:00
public ComponentState CrawlerState
{
get
{
if (stopping)
return ComponentState.STOPPING;
if (crawlThreadPool.CurrentPoolSize == 0)
return ComponentState.INITIALIZED;
return ComponentState.STARTED;
}
}
2019-03-13 08:20:53 +01:00
public void Enqueue(JobDelegate job)
{
crawlThreadPool.Enqueue(job);
}
2019-03-15 07:43:12 +01:00
public void Crawl(CIDR host)
{
crawlThreadPool.Enqueue(() => crawlHost(host.Host));
}
private void crawlHost(CIDR host)
2019-03-13 08:20:53 +01:00
{
2019-03-15 07:43:12 +01:00
CrawledHost crawledHost = CrawlPool.HostForIP(host);
crawledHost.LastCheck = DateTime.Now;
crawledHost.NextCheck = DateTime.Now + TimeSpan.FromHours(1);
if (crawlPing(crawledHost))
{
if (crawledHost.FirstSeen.Equals(DateTime.MinValue))
crawledHost.FirstSeen = DateTime.Now;
crawledHost.LastSeen = DateTime.Now;
CrawlSNMP(crawledHost);
}
else
{
}
2019-03-13 08:20:53 +01:00
}
2019-03-15 07:43:12 +01:00
public bool crawlPing(CrawledHost crawledHost)
2019-03-13 08:20:53 +01:00
{
Ping ping = new Ping();
2019-03-15 07:43:12 +01:00
int nSuccess = 0;
long roundTripTime = 0;
for (int n=0;n<10;n++)
{
PingReply pingReply = ping.Send(crawledHost.PrimaryIP, 500);
if (pingReply.Status == IPStatus.Success)
{
nSuccess++;
roundTripTime += pingReply.RoundtripTime;
}
}
if (nSuccess > 0)
2019-03-13 08:20:53 +01:00
{
2019-03-15 07:43:12 +01:00
roundTripTime /= nSuccess;
crawledHost.SetHint("ping.success", true);
crawledHost.SetHint("ping.rta", (int)roundTripTime);
crawledHost.SetHint("ping.out_of_ten", nSuccess);
Logging.Log(LogLevel.INFO, "Host is reachable: {0} RTA={1}ms", crawledHost.PrimaryIP, roundTripTime);
2019-03-13 08:20:53 +01:00
}
else
{
2019-03-15 07:43:12 +01:00
crawledHost.SetHint("ping.success", false);
crawledHost.SetHint("ping.rta", null);
crawledHost.SetHint("ping.out_of_ten", 0);
Logging.Log(LogLevel.INFO, "Host is unreachable: {0}", crawledHost.PrimaryIP);
}
return crawledHost.GetHint<bool>("ping.success");
}
public void CrawlSNMP(CrawledHost crawledHost)
{
string[] communities = new string[] { "VhclfC7lfIojYZ", "Vhclf(C7$lfIojYZ", "ByFR4oW98hap", "qVy3hnZJ2fov" };
bool snmpDetected = false;
2019-03-13 08:20:53 +01:00
2019-03-15 07:43:12 +01:00
using (USMEndpoint v3endpoint = new USMEndpoint(SNMPEngine, new IPEndPoint(crawledHost.PrimaryIP, 161)))
{
2019-03-13 08:20:53 +01:00
try
{
2019-03-15 07:43:12 +01:00
v3endpoint.QueryEngineID();
}
catch (TimeoutException)
{
}
2019-03-13 08:20:53 +01:00
2019-03-15 07:43:12 +01:00
if (v3endpoint.RemoteEngineID != null)
{
crawledHost.SetHint("snmp.version", 3);
Logging.Log(LogLevel.INFO, "{0}: SNMPv3 support detected", crawledHost.PrimaryIP);
bool replied = false;
foreach (string community in communities)
{
v3endpoint.Username = "skytron";
v3endpoint.AuthMethod = SnmpV3AuthMethod.SHA;
v3endpoint.AuthKeyPhrase = community;
try
{
Variable prID = v3endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
crawledHost.SetHint("snmp.username", "skytron");
crawledHost.SetHint("snmp.authkey", community);
crawledHost.SetHint("snmp.sysObjectID", (prID as ObjectIdentifier).AsString);
replied = true;
break;
}
catch (TimeoutException)
{
}
}
if (replied)
2019-03-13 08:20:53 +01:00
{
2019-03-15 07:43:12 +01:00
snmpDetected = true;
}
2019-03-13 08:20:53 +01:00
2019-03-15 07:43:12 +01:00
}
}
if (!snmpDetected)
{
using (SnmpV2Endpoint v2endpoint = new SnmpV2Endpoint(SNMPEngine, new IPEndPoint(crawledHost.PrimaryIP, 161)))
{
foreach (String community in communities)
{
v2endpoint.CommunityString = community;
try
{
Variable prID = v2endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
crawledHost.SetHint("snmp.version", 2);
crawledHost.SetHint("snmp.community", community);
crawledHost.SetHint("snmp.sysObjectID", (prID as ObjectIdentifier).AsString);
snmpDetected = true;
break;
} catch (TimeoutException)
{
}
}
}
}
if (!snmpDetected)
{
using (SnmpV1Endpoint v1endpoint = new SnmpV1Endpoint(SNMPEngine, new IPEndPoint(crawledHost.PrimaryIP, 161)))
{
foreach (String community in communities)
{
v1endpoint.CommunityString = community;
try
2019-03-13 08:20:53 +01:00
{
2019-03-15 07:43:12 +01:00
Variable prID = v1endpoint.snmpGet("1.3.6.1.2.1.1.2.0");
crawledHost.SetHint("snmp.version", 1);
crawledHost.SetHint("snmp.community", community);
crawledHost.SetHint("snmp.sysObjectID", (prID as ObjectIdentifier).AsString);
snmpDetected = true;
break;
}
catch (TimeoutException)
{
}
}
}
}
if (!snmpDetected)
{
crawledHost.SetHint("snmp.version", null);
crawledHost.SetHint("snmp.username", null);
crawledHost.SetHint("snmp.authkey", null);
crawledHost.SetHint("snmp.community", null);
crawledHost.SetHint("snmp.sysObjectID", null);
}
else
{
try
{
using (SnmpEndpoint endpoint = crawledHost.GetSnmpEndpoint(SNMPEngine))
{
try
{
Sequence[] VorIDs = endpoint.snmpWalk("1.3.6.1.2.1.1.9.1.2").ToArray();
string[] orids = new string[VorIDs.Length];
for (int n = 0; n < orids.Length; n++)
2019-03-13 08:20:53 +01:00
{
2019-03-15 07:43:12 +01:00
orids[n] = (VorIDs[n].Items[1] as ObjectIdentifier).AsString;
2019-03-13 08:20:53 +01:00
}
2019-03-15 07:43:12 +01:00
crawledHost.SetHint("snmp.orids", orids);
} catch (TimeoutException)
{ }
List<String> ids = new List<string>(crawledHost.GetHint<String[]>("snmp.orids", new string[0]));
if (ids.Contains("1.3.6.1.2.1.31") || crawledHost.GetHint<string>("snmp.sysObjectID","").Equals("1.3.6.1.4.1.14988.1"))
{
CrawlRFC1213(crawledHost, endpoint);
2019-03-13 08:20:53 +01:00
}
}
2019-03-15 07:43:12 +01:00
} catch (TimeoutException te)
2019-03-13 08:20:53 +01:00
{
2019-03-15 07:43:12 +01:00
Logging.Log(te);
2019-03-13 08:20:53 +01:00
}
2019-03-15 07:43:12 +01:00
}
}
private void CrawlRFC1213(CrawledHost crawledHost, SnmpEndpoint endpoint)
{
RFC1213.Interface[] interfaces = RFC1213.GetInterfaces(endpoint);
foreach (RFC1213.Interface netIf in interfaces)
{
foreach (CIDR ip in netIf.IPAddresses)
{
if (!crawledHost.IPAddresses.Contains(ip))
crawledHost.IPAddresses.Add(ip);
2019-03-13 08:20:53 +01:00
2019-03-15 07:43:12 +01:00
CrawlPool.GetSubnet(ip.Network);
}
2019-03-13 08:20:53 +01:00
}
}
2019-03-15 07:43:12 +01:00
public void CrawlSubnet(CIDR network)
2019-03-13 08:20:53 +01:00
{
Ping ping = new Ping();
2019-03-15 07:43:12 +01:00
Subnet subnet = CrawlPool.GetSubnet(network);
subnet.LastScan = DateTime.Now;
subnet.NextScan = DateTime.Now + TimeSpan.FromHours(6);
2019-03-13 08:20:53 +01:00
Logging.Log(LogLevel.INFO, "Scanning {0}", subnet);
2019-03-15 07:43:12 +01:00
foreach (CIDR ip in network)
2019-03-13 08:20:53 +01:00
{
long roundTripTime = 0;
int success = 0;
for (int n = 0; n < 4; n++)
{
PingReply reply = ping.Send(ip,250);
if (reply.Status == IPStatus.Success)
{
success++;
roundTripTime += reply.RoundtripTime;
}
}
if (success > 0)
{
roundTripTime /= success;
Logging.Log(LogLevel.INFO, "IP {0} reachable ({1}/10) {2}ms", ip, success, roundTripTime);
2019-03-15 07:43:12 +01:00
CrawlPool.HostForIP(ip);
2019-03-13 08:20:53 +01:00
}
}
}
2019-03-15 07:43:12 +01:00
private void scheduler()
2019-03-13 08:20:53 +01:00
{
while (!stopping)
{
2019-03-15 07:43:12 +01:00
foreach (CrawledHost crawledHost in CrawlPool.Hosts)
{
if (crawledHost.NextCheck < DateTime.Now)
{
Crawl(crawledHost.PrimaryIP);
}
}
2019-03-13 08:20:53 +01:00
2019-03-15 07:43:12 +01:00
foreach (Subnet subnet in CrawlPool.Subnets.ToArray())
2019-03-13 08:20:53 +01:00
{
2019-03-15 07:43:12 +01:00
if (subnet.NextScan < DateTime.Now)
2019-03-13 08:20:53 +01:00
{
2019-03-15 07:43:12 +01:00
Enqueue(() => CrawlSubnet(subnet.Network));
2019-03-13 08:20:53 +01:00
}
}
2019-03-15 07:43:12 +01:00
Thread.Sleep(15000);
2019-03-13 08:20:53 +01:00
}
}
2019-03-15 07:43:12 +01:00
///** PerfDB **/
2019-03-13 08:20:53 +01:00
2019-03-15 07:43:12 +01:00
//Dictionary<string, PerfFile> perfFiles = new Dictionary<string, PerfFile>();
2019-03-13 08:20:53 +01:00
2019-03-15 07:43:12 +01:00
//public PerfFile GetPerfFile(string name)
//{
// if (perfFiles.ContainsKey(name))
// return perfFiles[name];
2019-03-13 08:20:53 +01:00
2019-03-15 07:43:12 +01:00
// PerfFile perfFile = new PerfFile(Path.Combine(PerfPath, String.Format("{0}.perf", name)));
// perfFile.Open();
2019-03-13 08:20:53 +01:00
2019-03-15 07:43:12 +01:00
// perfFiles.Add(name, perfFile);
// if (perfFile.FirstSection == null)
// {
// PerfFile.PerfFileSection section = new PerfFile.PerfFileSection(perfFile, null, 1440, 60, AggregationMethod.AVERAGE);
// section = new PerfFile.PerfFileSection(perfFile, section, 1728, 300, AggregationMethod.AVERAGE);
// section = new PerfFile.PerfFileSection(perfFile, section, 2016, 900, AggregationMethod.AVERAGE);
// section = new PerfFile.PerfFileSection(perfFile, section, 1344, 3600, AggregationMethod.AVERAGE);
// section = new PerfFile.PerfFileSection(perfFile, section, 1344, 10800, AggregationMethod.AVERAGE);
// }
// return perfFile;
//}
2019-03-13 08:20:53 +01:00
}
}
2019-03-15 07:43:12 +01:00
// //SnmpV1Endpoint v1endpoint = new SnmpV1Endpoint(engine, new IPEndPoint(IPAddress.Parse("10.75.1.10"), 161), "ByFR4oW98hap");
// //SnmpV2Endpoint v2endpoint = new SnmpV2Endpoint(engine, new IPEndPoint(IPAddress.Parse("10.113.254.4"), 161), "ghE7wUmFPoPpkRno");
// USMEndpoint v3endpoint = new USMEndpoint(SNMPEngine, new IPEndPoint(host, 161));
// v3endpoint.AuthMethod = SnmpV3AuthMethod.SHA;
// v3endpoint.AuthKeyPhrase = "qVy3hnZJ2fov";
// v3endpoint.Username = "skytron";
// try
// {
// RFC1213.Interface[] interfaces = RFC1213.GetInterfaces(v3endpoint);
// foreach (RFC1213.Interface netIf in interfaces)
// {
// Logging.Log(LogLevel.INFO, "Interface: {0}", netIf);
// foreach (CIDR ip in netIf.IPAddresses)
// {
// Subnet subnet = CrawlPool.GetSubnet(ip.Network);
// if ((DateTime.Now - subnet.LastScanned).Hours >= 1)
// {
// Enqueue(() => crawlSubnet(ip.Network));
// }
// }
// }
// }
// catch (TimeoutException)
// {
// Logging.Log(LogLevel.INFO, "Host: {0} SNMP communication timed out.", host);
// }
//}