public Get ( SnmpVersion version, Pdu pdu ) : AsnType>.Dictionary |
||
version | SnmpVersion | SNMP protocol version. Acceptable values are SnmpVersion.Ver1 and SnmpVersion.Ver2 |
pdu | Pdu | Request Protocol Data Unit |
리턴 | AsnType>.Dictionary |
/// <summary> /// Update Ammo information /// </summary> public void UpdateAmmo() { SimpleSnmp snmp = new SimpleSnmp(snmpAgent, 16100, snmpCommunity, 2000, 2); // Create a request Pdu Pdu pdu = new Pdu(); pdu.Type = PduType.Get; pdu.VbList.Add(baseTreeOid + ".6.1.1"); //mg ammo 6 pdu.VbList.Add(baseTreeOid + ".6.2.1"); //missile ammo 8 pdu.VbList.Add(baseTreeOid + ".6.3.1"); //rail ammo 10 PrintPacketSend(pdu); Dictionary<Oid, AsnType> result = snmp.Get(SnmpVersion.Ver1, pdu); if (result == null) { Debug.Log("Manager:Set failed."); return; } List<AsnType> list = new List<AsnType>(result.Values); //guns machineGunAmmo = int.Parse(list[0].ToString()); missileLauncherAmmo = int.Parse(list[1].ToString()); railGunAmmo = int.Parse(list[2].ToString()); }
/// /// <summary> /// Finds the site name of the SNMP Agent. If site name is retreived, then SNMP commands can be issued. /// First tries a DVM command, then if it is not DVM, uses a DVMS command to see if it is instead a DVMS device. /// </summary> /// <param name="strHost">IP address</param> /// <param name="strComm">Community String</param> /// <returns>SNMP agent's Site Name</returns> public static string getSiteName(string strHost, string strComm) { SimpleSnmp snmp = new SimpleSnmp(strHost, strComm); if(!snmp.Valid) { return null; } Dictionary<Oid, AsnType> resultDVM = snmp.Get(SnmpVersion.Ver1, new string[] {".1.3.6.1.4.1.2566.127.1.1.157.3.1.1.1.0"}); if(resultDVM != null) { foreach(KeyValuePair<Oid, AsnType> kvp in resultDVM) { return kvp.Value.ToString(); } } //Now tries DVMS command Dictionary<Oid, AsnType> resultDVMs = snmp.Get(SnmpVersion.Ver1, new string[] { ".1.3.6.1.4.1.2566.127.1.1.152.3.1.1.1.0" }); if(resultDVMs != null) { foreach(KeyValuePair<Oid, AsnType> kvp in resultDVMs) { return kvp.Value.ToString(); } } return null; }
public bool SnmpGetTest(string host, string community, string oid) { try { SimpleSnmp snmp = new SimpleSnmp(host, community); if (!snmp.Valid) { return false; } Dictionary<Oid, AsnType> result = snmp.Get(SnmpVersion.Ver1, new string[] { oid }); if (result == null) { return false; } LogResult(result); return true; } catch (Exception ex) { return false; } }
private static string GetDeviceMessage(SimpleSnmp snmp) { //.1.3.6.1.2.1.25.3.2.1.5.1 var deviceStatus = Convert.ToInt32(snmp.Get(SnmpVersion.Ver2, new[] {".1.3.6.1.2.1.25.3.2.1.5.1"}) [new Oid(".1.3.6.1.2.1.25.3.2.1.5.1")].ToString()); string deviceMessage = null; switch (deviceStatus) { case 1: //nieznany deviceMessage = "Nieznany"; break; case 2: //pracuje deviceMessage = "Pracuje"; break; case 3: //ostrzeżenie deviceMessage = "Ostrzeżenie"; break; case 4: //testowy deviceMessage = "Stan testowy"; break; case 5: //wyłączony deviceMessage = "Niedostępny"; break; } return deviceMessage; }
/// <summary> /// Send a SNMP get request for the metal gear position /// </summary> public void GetPosition() { SimpleSnmp snmp = new SimpleSnmp(snmpAgent, 16100, snmpCommunity, 2000, 2); // Create a request Pdu Pdu pdu = new Pdu(); pdu.Type = PduType.Get; //pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); pdu.VbList.Add(baseTreeOid + ".2"); //location 2 PrintPacketSend(pdu); Dictionary<Oid, AsnType> result = snmp.Get(SnmpVersion.Ver1, pdu); //Debug.Log(result); //Dictionary<Oid, AsnType> result = snmp.GetNext(SnmpVersion.Ver1, pdu); if (result == null) { Debug.Log("Manager:Request failed."); } else { List<AsnType> list = new List<AsnType>(result.Values); location = list[0].ToString(); // send X:--- Y:--- var r = new Regex(@"[0-9]+\.[0-9]+"); var mc = r.Matches(location); var matches = new Match[mc.Count]; mc.CopyTo(matches, 0); var myFloats = new float[matches.Length]; float x = float.Parse(matches[0].Value); float y = float.Parse(matches[1].Value); metalGearMapPosition.rectTransform.anchoredPosition = new Vector2( x , y ); } }
/// <summary> /// Update enemies information /// </summary> public void UpdateEnemies() { SimpleSnmp snmp = new SimpleSnmp(snmpAgent, 16100, snmpCommunity, 2000, 0); // Create a request Pdu Pdu pdu = new Pdu(); pdu.Type = PduType.Get; //using a get next here would be the ideal ... but lets do the naive way for simplicity // asks for all the enemies around and put the values on a list, if some fail we can notice it by the list size pdu.VbList.Add(baseTreeOid + ".9.1.1"); pdu.VbList.Add(baseTreeOid + ".9.1.2"); pdu.VbList.Add(baseTreeOid + ".9.1.3"); pdu.VbList.Add(baseTreeOid + ".9.1.4"); Dictionary<Oid, AsnType> result = new Dictionary<Oid, AsnType>(); List<AsnType> list = new List<AsnType>(result.Values); PrintPacketSend(pdu); Dictionary<Oid, AsnType> result1 = snmp.Get(SnmpVersion.Ver1, pdu); if (result1 != null) { foreach (KeyValuePair<Oid, AsnType> entry in result1) { list.Add(entry.Value); } } SimpleSnmp snmp2 = new SimpleSnmp(snmpAgent, 16100, snmpCommunity, 2000, 0); pdu = new Pdu(); pdu.Type = PduType.Get; pdu.VbList.Add(baseTreeOid + ".9.2.1"); pdu.VbList.Add(baseTreeOid + ".9.2.2"); pdu.VbList.Add(baseTreeOid + ".9.2.3"); pdu.VbList.Add(baseTreeOid + ".9.2.4"); PrintPacketSend(pdu); Dictionary<Oid, AsnType> result2 = snmp2.Get(SnmpVersion.Ver1, pdu); if (result2 != null) { foreach (KeyValuePair<Oid, AsnType> entry in result2) { list.Add(entry.Value); } } SimpleSnmp snmp3 = new SimpleSnmp(snmpAgent, 16100, snmpCommunity, 2000, 0); pdu = new Pdu(); pdu.Type = PduType.Get; pdu.VbList.Add(baseTreeOid + ".9.3.1"); pdu.VbList.Add(baseTreeOid + ".9.3.2"); pdu.VbList.Add(baseTreeOid + ".9.3.3"); pdu.VbList.Add(baseTreeOid + ".9.3.4"); PrintPacketSend(pdu); Dictionary<Oid, AsnType> result3 = snmp3.Get(SnmpVersion.Ver1, pdu); if (result3 != null) { foreach (KeyValuePair<Oid, AsnType> entry in result3) { list.Add(entry.Value); } } if (result == null) { Debug.Log("Manager:Get enemies failed."); Enemy1.SetActive(false); Enemy2.SetActive(false); Enemy3.SetActive(false); return; } //parse the position strings Enemy1.SetActive(false); Enemy1.transform.GetChild(1).gameObject.SetActive(false); Enemy2.SetActive(false); Enemy2.transform.GetChild(1).gameObject.SetActive(false); Enemy3.SetActive(false); Enemy3.transform.GetChild(1).gameObject.SetActive(false); if(list.Count >= 4) { var r = new Regex(@"[0-9]+\.[0-9]+"); var mc = r.Matches(list[0].ToString()); var matches = new Match[mc.Count]; mc.CopyTo(matches, 0); var myFloats = new float[matches.Length]; float x = float.Parse(matches[0].Value); float y = float.Parse(matches[1].Value); Enemy1.GetComponent<Image>().rectTransform.anchoredPosition = new Vector2(x, y); Enemy1.GetComponent<Image>().rectTransform.localScale = new Vector2(1, 1) * int.Parse(list[1].ToString())*0.5f; if (int.Parse(list[3].ToString()) == 1) { Enemy1.transform.GetChild(2).gameObject.SetActive(true); } else { Enemy1.transform.GetChild(2).gameObject.SetActive(false); } Enemy1.SetActive(true); } if (list.Count >= 8) { var r = new Regex(@"[0-9]+\.[0-9]+"); var mc = r.Matches(list[4].ToString()); var matches = new Match[mc.Count]; mc.CopyTo(matches, 0); var myFloats = new float[matches.Length]; float x = float.Parse(matches[0].Value); float y = float.Parse(matches[1].Value); Enemy2.GetComponent<Image>().rectTransform.anchoredPosition = new Vector2(x, y); Enemy2.GetComponent<Image>().rectTransform.localScale = new Vector2(1, 1) * int.Parse(list[5].ToString()) * 0.5f; if (int.Parse(list[7].ToString()) == 1) { Enemy2.transform.GetChild(2).gameObject.SetActive(true); } else { Enemy2.transform.GetChild(2).gameObject.SetActive(false); } Enemy2.SetActive(true); } if (list.Count >= 12) { var r = new Regex(@"[0-9]+\.[0-9]+"); var mc = r.Matches(list[8].ToString()); var matches = new Match[mc.Count]; mc.CopyTo(matches, 0); var myFloats = new float[matches.Length]; float x = float.Parse(matches[0].Value); float y = float.Parse(matches[1].Value); Enemy3.GetComponent<Image>().rectTransform.anchoredPosition = new Vector2(x, y); Enemy3.GetComponent<Image>().rectTransform.localScale = new Vector2(1, 1) * int.Parse(list[9].ToString()) * 0.5f; if (int.Parse(list[11].ToString()) == 1) { Enemy3.transform.GetChild(2).gameObject.SetActive(true); } else { Enemy3.transform.GetChild(2).gameObject.SetActive(false); } Enemy3.SetActive(true); } //turn off trap light if it was on foundEnemy = 0; }
/// <summary> /// Ask the agent for all the health status /// </summary> public void UpdateHealth() { SimpleSnmp snmp = new SimpleSnmp(snmpAgent, 16100, snmpCommunity, 2000, 2); // Create a request Pdu Pdu pdu = new Pdu(); pdu.Type = PduType.Get; pdu.VbList.Add(baseTreeOid + ".13.1.1"); //head health 17 pdu.VbList.Add(baseTreeOid + ".13.1.2"); //head armor 18 pdu.VbList.Add(baseTreeOid + ".13.2.1"); // arm healt 13 pdu.VbList.Add(baseTreeOid + ".13.2.2"); // arm armor 20 pdu.VbList.Add(baseTreeOid + ".13.3.1"); //legs healt 21 pdu.VbList.Add(baseTreeOid + ".13.3.2"); //legs armor 22 PrintPacketSend(pdu); Dictionary<Oid, AsnType> result = snmp.Get(SnmpVersion.Ver1, pdu); if (result == null) { Debug.Log("Manager:Get failed."); return; } List<AsnType> list = new List<AsnType>(result.Values); //bodie ArmHealth = int.Parse(list[0].ToString()); ArmArmor = int.Parse(list[1].ToString()); LegsHealth = int.Parse(list[2].ToString()); LegsArmor = int.Parse(list[3].ToString()); HeadHealth = int.Parse(list[4].ToString()); HeadArmor = int.Parse(list[5].ToString()); ChangeBodyColors(); }
/// <summary> /// Called on the agent connection to initialize the variables on the manager /// </summary> public int StartUp() { SimpleSnmp snmp = new SimpleSnmp(snmpAgent, 16100, snmpCommunity, 2000, 2); // Create a request Pdu Pdu pdu = new Pdu(); pdu.Type = PduType.Get; //pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); pdu.VbList.Add(baseTreeOid + ".1"); //sysuptime 1 pdu.VbList.Add(baseTreeOid + ".2"); //location 2 pdu.VbList.Add(baseTreeOid + ".3"); //movex 3 pdu.VbList.Add(baseTreeOid + ".4"); //movey 4 pdu.VbList.Add(baseTreeOid + ".5"); //lookat 5 pdu.VbList.Add(baseTreeOid + ".6.1.1"); //mg ammo 6 pdu.VbList.Add(baseTreeOid + ".6.1.2"); //mg dam 7 pdu.VbList.Add(baseTreeOid + ".6.2.1"); //missile ammo 8 pdu.VbList.Add(baseTreeOid + ".6.2.2"); //missile dam 9 pdu.VbList.Add(baseTreeOid + ".6.3.1"); //rail ammo 10 pdu.VbList.Add(baseTreeOid + ".6.3.2"); //rail dam 11 pdu.VbList.Add(baseTreeOid + ".7"); //nukestate 12 pdu.VbList.Add(baseTreeOid + ".8"); //nukeLaunch 13 //.9 is radar table pdu.VbList.Add(baseTreeOid + ".10"); //radar state 14 //.11 is camera table ---DEPRECATED pdu.VbList.Add(baseTreeOid + ".12"); //camera select 16 pdu.VbList.Add(baseTreeOid + ".13.1.1"); //head health 17 pdu.VbList.Add(baseTreeOid + ".13.1.2"); //head armor 18 pdu.VbList.Add(baseTreeOid + ".13.2.1"); // arm healt 13 pdu.VbList.Add(baseTreeOid + ".13.2.2"); // arm armor 20 pdu.VbList.Add(baseTreeOid + ".13.3.1"); //legs healt 21 pdu.VbList.Add(baseTreeOid + ".13.3.2"); //legs armor 22 pdu.VbList.Add(baseTreeOid + ".14"); // selec target 23 pdu.VbList.Add(baseTreeOid + ".15"); // select gun 24 pdu.VbList.Add(baseTreeOid + ".16"); //attack 25 pdu.VbList.Add(baseTreeOid + ".17"); //under attack 26 Dictionary<Oid, AsnType> result = snmp.Get(SnmpVersion.Ver1, pdu); if (result == null) { Debug.Log("Manager:Request failed."); return 1; } else { List<AsnType> list = new List<AsnType>(result.Values); sysUpTime = uint.Parse(list[0].ToString()); location = list[1].ToString(); // send X:--- Y:--- lookAt = uint.Parse(list[4].ToString()); ; // rotate to x degrees //guns machineGunAmmo = int.Parse(list[5].ToString()); machineGunDamage = int.Parse(list[6].ToString()); GunAmmo1.GetComponent<Text>().text = machineGunAmmo.ToString(); GunDamage1.GetComponent<Text>().text = machineGunDamage.ToString(); missileLauncherAmmo= int.Parse(list[7].ToString()); missileLauncherDamage = int.Parse(list[8].ToString()); GunAmmo2.GetComponent<Text>().text = missileLauncherAmmo.ToString(); GunDamage2.GetComponent<Text>().text = missileLauncherDamage.ToString(); Debug.Log(list[9].ToString() + list[10].ToString()); railGunAmmo = int.Parse(list[9].ToString()); railGunDamage = int.Parse(list[10].ToString()); GunAmmo3.GetComponent<Text>().text = railGunAmmo.ToString(); GunDamage3.GetComponent<Text>().text = railGunDamage.ToString(); //cameras nukeState = uint.Parse(list[11].ToString()); nukeCounter = int.Parse(list[12].ToString()); radarState = uint.Parse(list[13].ToString()); selectedCamera = uint.Parse(list[14].ToString()); //bodie HeadHealth = int.Parse(list[15].ToString()); HeadArmor = int.Parse(list[16].ToString()); ArmHealth= int.Parse(list[17].ToString()); ArmArmor = int.Parse(list[18].ToString()); LegsHealth = int.Parse(list[19].ToString()); LegsArmor = int.Parse(list[20].ToString()); selectedTarget = int.Parse(list[21].ToString()); selectdGun = int.Parse(list[22].ToString()); attacking = int.Parse(list[23].ToString()); underAttack = int.Parse(list[24].ToString()); ChangeBodyColors(); GetPosition(); } return 0; }
private void WaitForCurrentConnectionsToDrain(string farm, string server, string snmpEndpoint, int snmpPort, string snmpCommunity, DateTime timeout) { if (DateTime.Now > timeout) throw new ConDepLoadBalancerException(string.Format("Timed out while taking {0} offline in load balancer.", server)); Logger.Verbose("Connecting to snmp using " + snmpEndpoint + " on port " + snmpPort + " with community " + snmpCommunity); var snmp = new SimpleSnmp(snmpEndpoint, snmpPort, snmpCommunity); Logger.Verbose("Getting snmp info about farm " + farm); var farmResult = snmp.Walk(SnmpVersion.Ver2, FARM_NAMES_OID); var farmOid = farmResult.Single(x => x.Value.Clone().ToString() == farm); var id = farmOid.Key.ToString(); var start = farmOid.Key.ToString().LastIndexOf("."); var farmSubId = id.Substring(start + 1, id.Length - start - 1); Logger.Verbose("Getting snmp info about server " + server); var serverResult = snmp.Walk(SnmpVersion.Ver2, SERVER_NAMES_OID + farmSubId); var serverOid = serverResult.Single(x => x.Value.Clone().ToString() == server); var serverId = serverOid.Key.ToString(); start = serverOid.Key.ToString().LastIndexOf("."); var serverSubId = serverId.Substring(start + 1, serverId.Length - start - 1); Logger.Verbose("Getting current server sessions for server " + server); var pdu = Pdu.GetPdu(); pdu.VbList.Add(SERVER_CUR_SESSIONS_OID + farmSubId + "." + serverSubId); var currentSessionsVal = snmp.Get(SnmpVersion.Ver2, pdu); var val = currentSessionsVal.Single().Value.Clone() as Counter64; if (val > 0) { Logger.Verbose("Server " + server + " has " + val + " active sessions."); var waitInterval = 3; Logger.Warn(string.Format("There are still {0} active connections on server {1}. Waiting {2} second before retry.", val, server, waitInterval)); Thread.Sleep(1000 * waitInterval); WaitForCurrentConnectionsToDrain(farm, server, snmpEndpoint, snmpPort, snmpCommunity, timeout); } Logger.Verbose("Server " + server + " has 0 active session and is now offline."); }
private static string GetRestOfMessages(SimpleSnmp snmp) { var otherStatuses = Encoding.ASCII.GetBytes(snmp.Get(SnmpVersion.Ver2, new[] { ".1.3.6.1.2.1.25.3.5.1.2.1" }) [new Oid(".1.3.6.1.2.1.25.3.5.1.2.1")].ToString()); var otherStatus = otherStatuses[0]; var setFlags = Enum.ToObject(typeof (OtherEnum), otherStatus).ToString(); var flagsList = new List<string>(); if(setFlags.Contains(",")) { flagsList = setFlags.Replace(" ", string.Empty).Split(',').ToList(); } else { flagsList.Add(setFlags); } var otherMessage = ""; for (var i = 0; i < flagsList.Count; i++) { switch (flagsList[i]) { case "ServiceRequested": otherMessage += "Potrzebny serwis"; break; case "Offline": otherMessage += "Offline"; break; case "Jammed": otherMessage += "Zablokowany"; break; case "DoorOpen": otherMessage += "Otwarte drzwiczki"; break; case "NoToner": otherMessage += "Brak tonera"; break; case "LowToner": otherMessage += "Mało tonera"; break; case "NoPaper": otherMessage += "Brak papieru"; break; case "LowPaper": otherMessage += "Mało papieru"; break; } if (flagsList.Count > 1 && i < flagsList.Count - 1) { otherMessage += ", "; } } return otherMessage; }
private static string GetPrinterMessage(SimpleSnmp snmp) { //.1.3.6.1.2.1.25.3.5.1.1.1 var printerStatus = Convert.ToInt32(snmp.Get(SnmpVersion.Ver2, new[] {".1.3.6.1.2.1.25.3.5.1.1.1"}) [new Oid(".1.3.6.1.2.1.25.3.5.1.1.1")].ToString()); string printerMessageStatus = null; switch (printerStatus) { case 1: //inny printerMessageStatus = "Inny"; break; case 2: //nieznany printerMessageStatus = "Nieznany"; break; case 3: //zajęta printerMessageStatus = "Zajęta"; break; case 4: //drukuje printerMessageStatus = "Drukuje"; break; case 5: //rozgrzewa się printerMessageStatus = "Rozgrzewa się"; break; } return printerMessageStatus; }