using SnmpSharpNet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace SNMP_TRAP_SEND
{
class Program
{
// 로거 ILog 필드
//log4net.Config.XmlConfigurator.Configure();
private static ILog log = LogManager.GetLogger("Program");
public static string _IP_ADDRESS_SENDER ="192.168.0.46";
public static string _IP_ADDRESS = "192.168.0.55";
static PerformanceCounter cpu = new PerformanceCounter("Processor","% Processor Time","_Total");
static PerformanceCounter totalRAM = new PerformanceCounter("Memory","% Committed Bytes In Use");
static PerformanceCounter ram = new PerformanceCounter("Memory", "Committed Bytes");
static Double diskSize = 0;
static Double diskValue = 0;
static void Main(string[] args)
{
StreamWriter file = null;
try
{
string dateString =string.Format("{0:yyyyMMdd}",DateTime.Now);
string path = @"C:\WebSite\beeCloudPro\Logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
IPHostEntry host = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName());
string myip = host.AddressList[host.AddressList.Length - 1].ToString();
_IP_ADDRESS_SENDER = myip;
using (file)
{
VbCollection col = new VbCollection();
Vb c = new Vb();
Vb b = new Vb();
while (true)
{
col.Clear();
TrapAgent agent = new TrapAgent();
if (args.LongLength > 0)
{
// _IP_ADDRESS_SENDER = args[0];
_IP_ADDRESS= args[0];
}
if (args.Length > 1)
{
_IP_ADDRESS = args[1];
}
string cpuValue = cpu.NextValue().ToString();
string totalRamValue = totalRAM.NextValue().ToString();
string ramValue = ram.NextValue().ToString();
string now;
now = string.Format("{0:yyyy-MM-dd HH:mm:ss}",DateTime.Now);
DriveInfo[] allDrives = DriveInfo.GetDrives();
for(int i=0;i<allDrives.Length;i++)
{
DriveInfo a = allDrives[i];
if (a.Name.Equals("C:\\"))
{
diskSize = a.TotalSize - a.TotalFreeSpace;
diskValue = diskSize / a.TotalSize * 100;
}
}
string line = string.Format("{0}|{1}%|{2}%({3}GB)|{4}%({5}GB)"
, now
, Math.Round(Convert.ToDouble(cpuValue), 2)
, Math.Round(Convert.ToDouble(totalRamValue), 2)
, Math.Round(Convert.ToDouble(ramValue)/1024 / 1024 / 1024, 2) //RAM GB
, Math.Round(diskValue, 2)
, Math.Round(diskSize / 1024 / 1024 / 1024, 2)
);
string diskValueTemp = string.Format("{0} DISK Used:{1}%({2}GB)"
,now
, Math.Round(diskValue, 2)
, Math.Round(diskSize / 1024 / 1024 / 1024, 2));
cpuValue = string.Format("{0} CPU Used:{1}%"
, now
, Math.Round(Convert.ToDouble(cpuValue), 2));
ramValue = string.Format("{0} DISK Used:{1}%({2}GB)"
, now
, Math.Round(Convert.ToDouble(totalRamValue), 2)
, Math.Round(Convert.ToDouble(ramValue) / 1024 / 1024 / 1024, 2));
col.Add(new Oid("CPU"), new OctetString(cpuValue));
col.Add(new Oid("RAM"), new OctetString(ramValue));
col.Add(new Oid("DISK"), new OctetString(diskValueTemp));
//file.WriteLineAsync(line);
//file.Flush();
SnmpV1TrapPacket packet = new SnmpV1TrapPacket();
packet.Pdu.VbList.Add(col);
agent.SendV1Trap(packet, new IpAddress(_IP_ADDRESS_SENDER),162);
// OR
agent.SendV1Trap(new IpAddress(_IP_ADDRESS), 162,"public", new Oid("1.3.6.1.2.1.1.1.0"),new IpAddress(_IP_ADDRESS_SENDER),SnmpConstants.LinkUp,0,13432,col);
Console.WriteLine(line);
log.Info(line);
System.Threading.Thread.Sleep(3000);
}
}
}
catch(Exception e) { Console.WriteLine(e.Message); }
}
}
}
/************[ Log4net config in App.config ]*******************************/
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
<appSettings>
<add key="Master" value="IP"/>
</appSettings>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<File value="C:\WebSite\beeCloudPro\Logs\Server_Resources" />
<appendToFile value="true" />
<datePattern value="'_'yyyyMMdd'.log'" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<layout type="log4net.Layout.PatternLayout">
<!-- <param name="ConversionPattern" value="%date [%logger] %message %newline" />-->
<conversionPattern value="%message %newline" />
</layout>
</appender>
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<localPort value="162" />
<remoteAddress value="192.168.0.22" />
<remotePort value="162" />
<layout type="log4net.Layout.PatternLayout, log4net">
<conversionPattern value="%-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="Info"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
</configuration>
댓글 없음:
댓글 쓰기