• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

SNMP query with cfx_SNMP.java

New Here ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

Good afternoon.  I am currently using cfx_SNMP.java to enable SNMP queries/etc from my Coldfusion Webpages.  This code was built back in 2009.  I believe it is using SNMP v1.  I am introducing new devices into my system that will not respond with SNMP v1 so I am needing to incorporate this code into using v2c.  I am  not sure how to do this and was wondering if I could get somme pointers.  I ahve attached my cfc_SNMP.java code file to this request.  Thank you.

// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 5/13/2010 4:59:22 PM
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   cfx_SNMP.java

import com.allaire.cfx.*;
import java.net.InetAddress;
import java.util.StringTokenizer;
import snmp.*;

public class cfx_SNMP
    implements CustomTag
{

    public cfx_SNMP()
    {
    }

    public void processRequest(Request request, Response response)
        throws Exception
    {
        try
        {
            if(!request.attributeExists("COMMUNITY") || !request.attributeExists("HOST") || !request.attributeExists("OID") || !request.attributeExists("R_QRESULTS"))
                throw new Exception("Missing attribute(s) (COMMUNITY,HOST,R_QRESULTS AND OID are required attributes for this tag)");
            String s = request.getAttribute("R_QRESULTS");
            String s1 = request.getAttribute("COMMUNITY");
            String s3 = request.getAttribute("HOST");
            String s4 = request.getAttribute("OID");
            String s5 = "";
            int i = 30;
            if(request.attributeExists("TIMEOUT"))
            {
                String s6 = request.getAttribute("TIMEOUT");
                i = Integer.parseInt(s6);
            }
            i *= 1000;
            String as[] = {
                "OID", "Value", "Type", "Hex"
            };
            Query query = response.addQuery(s, as);
            int j = 1;
            byte byte0 = 2;
            byte byte1 = 3;
            byte byte2 = 4;
            int k = 0;
            InetAddress inetaddress = InetAddress.getByName(s3);
            SNMPv1CommunicationInterface snmpv1communicationinterface = new SNMPv1CommunicationInterface(k, inetaddress, s1);
            snmpv1communicationinterface.setSocketTimeout(i);
            String s7 = "";
            String s9 = s4;
            String s11 = s9;
            String s13 = s4;
            String as1[] = readTokens(s13, ",");
            String s14 = "";
label0:
            for(int l = 0; l < as1.length; l++)
            {
                String s8 = "";
                String s10 = as1[l];
                String s12 = s10;
                try
                {
                    do
                    {
                        if(s10.equals(s8) || !s10.startsWith(s12) && !s10.equals(s12))
                            continue label0;
                        s8 = s10;
                        SNMPVarBindList snmpvarbindlist = snmpv1communicationinterface.getNextMIBEntry(s8);
                        SNMPSequence snmpsequence = (SNMPSequence)snmpvarbindlist.getSNMPObjectAt(0);
                        SNMPObjectIdentifier snmpobjectidentifier = (SNMPObjectIdentifier)snmpsequence.getSNMPObjectAt(0);
                        SNMPObject snmpobject = snmpsequence.getSNMPObjectAt(1);
                        s10 = snmpobjectidentifier.toString();
                        if(!s10.startsWith(s12) && !s10.equals(s12))
                            break;
                        String s15 = snmpobject.getClass().getName();
                        s14 = s10;
                        if(s15.equals("snmp.SNMPOctetString"))
                        {
                            String s17 = snmpobject.toString();
                            int i1 = s17.indexOf('\0');
                            if(i1 >= 0)
                                s17 = s17.substring(0, i1);
                            int i2 = query.addRow();
                            query.setData(i2, j, snmpobjectidentifier.toString());
                            query.setData(i2, byte0, s17);
                            query.setData(i2, byte1, s15.substring(9, s15.length()));
                            query.setData(i2, byte2, ((SNMPOctetString)snmpobject).toHexString());
                        } else
                        {
                            String s18 = snmpobject.toString();
                            int j1 = s18.indexOf('\0');
                            if(j1 >= 0)
                                s18 = s18.substring(0, j1);
                            int j2 = query.addRow();
                            query.setData(j2, j, snmpobjectidentifier.toString());
                            query.setData(j2, byte0, s18);
                            query.setData(j2, byte1, s15.substring(9, s15.length()));
                            query.setData(j2, byte2, "");
                        }
                    } while(true);
                    SNMPVarBindList snmpvarbindlist1 = snmpv1communicationinterface.getMIBEntry(s8);
                    SNMPSequence snmpsequence1 = (SNMPSequence)snmpvarbindlist1.getSNMPObjectAt(0);
                    SNMPObjectIdentifier snmpobjectidentifier1 = (SNMPObjectIdentifier)snmpsequence1.getSNMPObjectAt(0);
                    SNMPObject snmpobject1 = snmpsequence1.getSNMPObjectAt(1);
                    s10 = snmpobjectidentifier1.toString();
                    if(s10.equals(s14))
                        continue;
                    String s16 = snmpobject1.getClass().getName();
                    if(s16.equals("snmp.SNMPOctetString"))
                    {
                        String s19 = snmpobject1.toString();
                        int k1 = s19.indexOf('\0');
                        if(k1 >= 0)
                            s19 = s19.substring(0, k1);
                        int k2 = query.addRow();
                        query.setData(k2, j, snmpobjectidentifier1.toString());
                        query.setData(k2, byte0, s19);
                        query.setData(k2, byte1, s16.substring(9, s16.length()));
                        query.setData(k2, byte2, ((SNMPOctetString)snmpobject1).toHexString());
                        continue;
                    }
                    String s20 = snmpobject1.toString();
                    int l1 = s20.indexOf('\0');
                    if(l1 >= 0)
                        s20 = s20.substring(0, l1);
                    int l2 = query.addRow();
                    query.setData(l2, j, snmpobjectidentifier1.toString());
                    query.setData(l2, byte0, s20);
                    query.setData(l2, byte1, s16.substring(9, s16.length()));
                    query.setData(l2, byte2, "");
                }
                catch(Exception exception1) { }
            }

            snmpv1communicationinterface.closeConnection();
        }
        catch(Exception exception)
        {
            if(exception.toString().indexOf("snmp") != -1 || exception.toString().indexOf("timeout") != -1)
            {
                if(request.attributeExists("R_SNMP_ERRORS"))
                {
                    String s2 = request.getAttribute("R_SNMP_ERRORS");
                    response.setVariable(s2, exception.toString().substring(exception.toString().indexOf(": ") + 1, exception.toString().length()));
                }
            } else
            {
                throw new Exception(exception);
            }
        }
    }

    public static String[] readTokens(String s, String s1)
    {
        StringTokenizer stringtokenizer = new StringTokenizer(s, s1);
        int i = stringtokenizer.countTokens();
        String as[] = new String[i];
        for(int j = 0; j < i; j++)
            as[j] = stringtokenizer.nextToken();

        return as;
    }
}

Views

141

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2021 Apr 02, 2021

Copy link to clipboard

Copied

While I have no experience at all with that java code for snmp, I have a couple of questions that may help you with your challenge.

 

First, are you really saying that you are connecting to the new devices from an instance of cf where you had and can now successfully connect to other current devices? If not, the problem may not really be about the news devicds and their version, but instead may be a config problem for cf and your cfx, or cf's version.

 

And what IS your cf version and update level? And the jvm update level? These are all on the cf admin settings summary page, or can be seen in a cf dump of the server scope.

 

Sometimes, some problems of calling out to things can indeed be resolved just by updating the Java that cf is using. I have a blog post on this:

 

https://coldfusion.adobe.com/2019/06/error-calling-cf-via-https-solved-updating-jvm/

 

But I realize this may not prove to be your solution. Please let us know. And perhaps someone with more java and snmp experience may better help with your direct question, if that's the only solution  for you. 


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 04, 2021 Apr 04, 2021

Copy link to clipboard

Copied

LATEST

My first impression is: why? Nowhere in modern-day, post CFMX, ColdFusion do I see a need to use an SNMP custom tag.  There are various specialist tools you can use to monitor your network.

 

Could you tell us which information you wish to obtain from your ColdFusion application? Then the forum would share with you experiences with current network monitoring tools.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation