209 lines
8.1 KiB
C#
209 lines
8.1 KiB
C#
using System;
|
|
using MySql.Data.MySqlClient;
|
|
using ln.logging;
|
|
using ln.skyscanner.entities;
|
|
using System.Linq;
|
|
using ln.types.net;
|
|
using ln.types;
|
|
using System.Globalization;
|
|
using ln.types.odb.ng;
|
|
namespace ln.skyscanner.import.skytron
|
|
{
|
|
public class SkytronImport
|
|
{
|
|
MySqlConnection mysql;
|
|
|
|
public SkytronImport(String connectionString)
|
|
{
|
|
mysql = new MySqlConnection(connectionString);
|
|
}
|
|
|
|
public void Import(Mapper mapper)
|
|
{
|
|
try
|
|
{
|
|
mysql.Open();
|
|
|
|
ImportStammdaten(mapper);
|
|
ImportNodes(mapper);
|
|
//ImportL2Segmente(mapper);
|
|
|
|
} catch (Exception e)
|
|
{
|
|
Logging.Log(e);
|
|
|
|
if (mysql != null)
|
|
mysql.Close();
|
|
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void ImportStammdaten(Mapper mapper)
|
|
{
|
|
MySqlCommand command = mysql.CreateCommand();
|
|
|
|
command.CommandText = "SELECT * FROM sender_stammdaten";
|
|
using (MySqlDataReader reader = command.ExecuteReader())
|
|
{
|
|
|
|
while (reader.Read())
|
|
{
|
|
PointOfPresence pop = mapper.Load<PointOfPresence>(Query.Equals<PointOfPresence>("ForeignName", reader.GetInt32("senderid").ToString())).FirstOrDefault();
|
|
if (pop == null)
|
|
{
|
|
Logging.Log(LogLevel.INFO, "New PoP: {0} / {1}", reader.GetInt32("senderid"), reader.GetString("stationsname"));
|
|
pop = new PointOfPresence();
|
|
pop.ForeignName = reader.GetInt32("senderid").ToString();
|
|
pop.Name = reader.GetString("stationsname");
|
|
|
|
|
|
String lati = reader.GetString("koordinaten_breite");
|
|
String longi = reader.GetString("koordinaten_laenge");
|
|
|
|
if (!String.Empty.Equals(lati) && !String.Empty.Equals(longi))
|
|
pop.GeoLocation = new GeoLocation(float.Parse(lati,CultureInfo.InvariantCulture.NumberFormat),float.Parse(longi, CultureInfo.InvariantCulture.NumberFormat));
|
|
|
|
mapper.Save(pop);
|
|
}
|
|
else
|
|
{
|
|
Logging.Log(LogLevel.INFO, "Known PoP: {0}",pop);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ImportNodes(Mapper mapper)
|
|
{
|
|
MySqlCommand command = mysql.CreateCommand();
|
|
|
|
command.CommandText = "SELECT * FROM sender_ip_discovery";
|
|
using (MySqlDataReader reader = command.ExecuteReader())
|
|
{
|
|
|
|
while (reader.Read())
|
|
{
|
|
String id = reader.GetString("id");
|
|
IPv4 primaryIP = IPv4.Parse(reader.GetString("ip"));
|
|
|
|
Node node = mapper.Load<Node>(Query.Equals<Node>("uniqueIdentity", id)).FirstOrDefault();
|
|
if (node == null)
|
|
{
|
|
node = new Node(primaryIP);
|
|
node.UniqueIdentity = id;
|
|
Logging.Log(LogLevel.INFO, "New Node: {0} / {1}",id,primaryIP);
|
|
}
|
|
|
|
node.Vendor = reader.GetString("hersteller");
|
|
node.Product = reader.GetString("produkt");
|
|
node.Comment = reader.GetString("devicename");
|
|
|
|
if (reader.GetInt32("http_port") != 0)
|
|
{
|
|
string proto = reader.GetInt32("http_port") == 443 ? "https" : "http";
|
|
node.RemoveURI("http");
|
|
node.RemoveURI("https");
|
|
node.AddURI(new URI(String.Format("{2}://{0}:{1}",
|
|
primaryIP,
|
|
reader.GetString("http_port"),
|
|
proto
|
|
)));
|
|
}
|
|
if (reader.GetInt32("ssh_port") != 0)
|
|
{
|
|
node.RemoveURI("ssh");
|
|
node.AddURI(new URI(String.Format("ssh://{0}:{1}@{2}:{3}",
|
|
reader.GetString("ssh_user"),
|
|
reader.GetString("ssh_passwort"),
|
|
primaryIP,
|
|
reader.GetString("ssh_port")
|
|
)));
|
|
}
|
|
|
|
node.RemoveURI("snmp");
|
|
if (!String.Empty.Equals(reader.GetString("snmp_community")))
|
|
{
|
|
node.AddURI(new URI(String.Format("snmp://{0}@{1}#1",
|
|
reader.GetString("snmp_community"),
|
|
primaryIP
|
|
)));
|
|
}
|
|
if (!String.Empty.Equals(reader.GetString("snmp_v3_user")))
|
|
{
|
|
node.AddURI(new URI(String.Format("snmp://{0}:{1}:{2}@{3}#3",
|
|
reader.GetString("snmp_v3_user"),
|
|
reader.GetString("snmp_v3_auth_password"),
|
|
reader.GetString("snmp_v3_encrypt_password"),
|
|
primaryIP
|
|
)));
|
|
}
|
|
|
|
node.Severity = CheckSeverity.PRODUCTION;
|
|
|
|
mapper.Save(node);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ImportL2Segmente(Mapper mapper)
|
|
{
|
|
MySqlCommand command = mysql.CreateCommand();
|
|
|
|
command.CommandText = "SELECT * FROM sender_ip_subnetze";
|
|
|
|
using (MySqlDataReader reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
Network4 net4 = Network4.Parse(reader.GetString("cidr"));
|
|
L2Segment l2 = mapper.Load<L2Segment>(Query.Equals<L2Segment>("Network", net4)).FirstOrDefault();
|
|
if (l2 == null)
|
|
{
|
|
l2 = new L2Segment();
|
|
l2.Name = String.Format("SUB#{0}", reader.GetInt32("id").ToString());
|
|
l2.Comment = reader.GetString("beschreibung");
|
|
l2.Network = net4;
|
|
|
|
Logging.Log(LogLevel.INFO, "New L2Segment: {0} / {1}",net4,l2.Name);
|
|
|
|
String sid = reader.GetString("senderid1");
|
|
if ((sid != null) && !String.Empty.Equals(sid))
|
|
{
|
|
PointOfPresence pop = mapper.Load<PointOfPresence>(Query.Equals<PointOfPresence>("ForeignName", sid)).FirstOrDefault();
|
|
if (pop != null)
|
|
l2.PoPs.Add(pop);
|
|
}
|
|
sid = reader.GetString("senderid2");
|
|
if ((sid != null) && !String.Empty.Equals(sid))
|
|
{
|
|
PointOfPresence pop = mapper.Load<PointOfPresence>(Query.Equals<PointOfPresence>("ForeignName", sid)).FirstOrDefault();
|
|
if (pop != null)
|
|
l2.PoPs.Add(pop);
|
|
}
|
|
sid = reader.GetString("senderid3");
|
|
if ((sid != null) && !String.Empty.Equals(sid))
|
|
{
|
|
PointOfPresence pop = mapper.Load<PointOfPresence>(Query.Equals<PointOfPresence>("ForeignName", sid)).FirstOrDefault();
|
|
if (pop != null)
|
|
l2.PoPs.Add(pop);
|
|
}
|
|
sid = reader.GetString("senderid4");
|
|
if ((sid != null) && !String.Empty.Equals(sid))
|
|
{
|
|
PointOfPresence pop = mapper.Load<PointOfPresence>(Query.Equals<PointOfPresence>("ForeignName", sid)).FirstOrDefault();
|
|
if (pop != null)
|
|
l2.PoPs.Add(pop);
|
|
}
|
|
|
|
mapper.Save(l2);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|