Tuesday, February 1, 2011

SNMP C# for getting Printer Information

This C# Segment was written to get Printer Information in terms of cartridge availability.  This uses the existing library SNMPNetSharp.  This function will find out the serial number and output graphs of the % remaining of each Cyan, Magenta, Yellow and Black cartridges.  Also attached is screenshot of implementation in my MobileExec 2011 Application.



public void SNMPGetPrinter(string arg1, string serv1)
    {
        OctetString community = new OctetString(arg1);


        AgentParameters param = new AgentParameters(community);
        param.Version = SnmpVersion.Ver1;
        IpAddress agent = new IpAddress(serv1);


        UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);


        Pdu pdu = new Pdu(PduType.Get);
        pdu.VbList.Add("1.3.6.1.2.1.1.5.0"); //sysName 0
        pdu.VbList.Add("1.3.6.1.2.1.43.5.1.1.17.1"); //serial number 1
        pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.6.1.1"); //Black Descr 2
        pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.6.1.2"); //Cyan Descr 3
        pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.6.1.3"); //Yellow Descr 4
        pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.6.1.4"); //Magenta Descr 5


        pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.9.1.1"); //Black R 6
        pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.9.1.2"); //Cyan R 7
        pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.9.1.3"); //Yellow R 8 
        pdu.VbList.Add("1.3.6.1.2.1.43.11.1.1.9.1.4"); //Magenta R 9


        
        SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
        if (result != null)
        {


            if (result.Pdu.ErrorStatus != 0)
            {
                Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
                    result.Pdu.ErrorStatus,
                    result.Pdu.ErrorIndex);
          //Label1.Text += "Error:" + result.Pdu.ErrorStatus + ", " + result.Pdu.ErrorIndex;
            }
            else
            {
                string name1 = result.Pdu.VbList[0].Value.ToString();
                string serial1 = result.Pdu.VbList[1].Value.ToString();
                
                string bDesc = result.Pdu.VbList[2].Value.ToString();
                string cDesc = result.Pdu.VbList[3].Value.ToString();
                string yDesc = result.Pdu.VbList[4].Value.ToString();
                string mDesc = result.Pdu.VbList[5].Value.ToString();
                
                string bPer = result.Pdu.VbList[6].Value.ToString();
                string cPer = result.Pdu.VbList[7].Value.ToString();
                string yPer = result.Pdu.VbList[8].Value.ToString();
                string mPer = result.Pdu.VbList[9].Value.ToString();


                Int16 bPer1 = Convert.ToInt16(bPer);
                Int16 cPer1 = Convert.ToInt16(cPer);
                Int16 yPer1 = Convert.ToInt16(yPer);
                Int16 mPer1 = Convert.ToInt16(mPer);


                Label1.Text += "<b>" + name1 + "</b> cartridge consumption:";
                Label1.Text += "<table>";
                Label1.Text += "<tr><td BGCOLOR=\"DarkGray\"><font color='white'><b>Serial</b></td><td BGCOLOR=\"Silver\">" + serial1 + "</td><td BGCOLOR=\"Silver\">_ _ _ _ _ _ _</td></tr>";
                Label1.Text += "<tr><td BGCOLOR=\"DarkGray\"><font color='white'><b>" + bDesc + "</b></td><td BGCOLOR=\"Silver\">" + bPer + "% free</td>"
                   + "<td BGCOLOR=\"Silver\"><img src='images/black.png' height='29' width='" + bPer1 + "%'>" + "</td></tr>";
                Label1.Text += "<tr><td BGCOLOR=\"DarkGray\"><font color='white'><b>" + cDesc + "</b></td><td BGCOLOR=\"Silver\">" + cPer + "% free</td>"
                      + "<td BGCOLOR=\"Silver\"><img src='images/cyan.png' height='29' width='" + cPer1 + "%'>" + "</td></tr>";
                Label1.Text += "<tr><td BGCOLOR=\"DarkGray\"><font color='white'><b>" + yDesc + "</b></td><td BGCOLOR=\"Silver\">" + yPer + "% free</td>"
                      + "<td BGCOLOR=\"Silver\"><img src='images/magenta.png' height='29' width='" + mPer1 + "%'>" + "</td></tr>";
                Label1.Text += "<tr><td BGCOLOR=\"DarkGray\"><font color='white'><b>" + mDesc + "</b></td><td BGCOLOR=\"Silver\">" + mPer + "% free</td>"
                      + "<td BGCOLOR=\"Silver\"><img src='images/yellow.png' height='29' width='" + yPer1 + "%'>" + "</td></tr>";
                Label1.Text += "</table>";
    
            }
        }
    }

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...