Add new asset fields
Subscribe

From OpenNMS

Jump to: navigation, search

In order to add new asset fields to OpenNMS you have to get the OpenNMS source code and then modify a couple of files. This example will add fields for "cpu" and "ram". Just open the file in an editor, read my instructions and you should be able to find the place to modify pretty easily.

Contents

opennms-model/src/main/java/org/opennms/netmgt/model/OnmsAssetRecord.java

In the beginning of the file there are some declarations, add your fields here:

    /** identifier field */
    private String m_cpu;

    /** identifier field */
    private String m_ram;

Then, for each field you will have to get add set- and get methods.

    /**
     * <p>getCpu</p>
     *
     * @return a {@link java.lang.String} object.
     */
    @Column(name="cpu", length=1)
    public String getCpu() {
        return m_cpu;
    }

    /**
     * <p>setCpu</p>
     *
     * @param cpu a {@link java.lang.String} object.
     */
    public void setCpu(String cpu) {
            m_cpu = cpu;
    }

    /**
     * <p>getRam</p>
     *
     * @return a {@link java.lang.String} object.
     */
    @Column(name="ram", length=1)
    public String getRam() {
        return m_ram;
    }

    /**
     * <p>setRam</p>
     *
     * @param ram a {@link java.lang.String} object.
     */
    public void setRam(String ram) {
            m_ram = ram;
    }

and finally, there's a string you will have to append your fields to:

            .append("cpu", getCpu())
            .append("ram", getRam())

opennms-webapp/src/main/java/org/opennms/web/asset/ExportAssetsServlet.java

Append your fields to the list of strings, make sure you add a comma after the original last entry.

                "Cpu",
                "Ram"

Then add your fields to the entries:

            entries.add(asset.getCpu());
            entries.add(asset.getRam());

opennms-webapp/src/main/java/org/opennms/web/asset/Asset.java

Declare your fields:

    protected String cpu ="";
    
    protected String ram ="";

And once again, define get- and set-methods:

    /**
     * <p>Getter for the field <code>cpu</code>.</p>
     *
     * @return a {@link java.lang.String} object.
     */
    public String getCpu() {
        return cpu;
    }

    /**
     * <p>Setter for the field <code>cpu</code>.</p>
     *
     * @param cpu a {@link java.lang.String} object.
     */
    public void setCpu(String cpu) {
        if (cpu != null) {
            this.cpu = cpu;
        } else {
            this.cpu = "";
        }        
      }

    /**
     * <p>Getter for the field <code>ram</code>.</p>
     *
     * @return a {@link java.lang.String} object.
     */
    public String getRam() {
        return ram;
    }

    /**
     * <p>Setter for the field <code>ram</code>.</p>
     *
     * @param ram a {@link java.lang.String} object.
     */
    public void setRam(String ram) {
        if (ram != null) {
            this.ram = ram;
        } else {
            this.ram = "";
        }        
      }

opennms-webapp/src/main/java/org/opennms/web/asset/AssetModel.java

Enhance the prepared statement with your fields in the field list and additional ",?" in the values list

PreparedStatement stmt = conn.prepareStatement("INSERT INTO ASSETS 
(nodeID,category,manufacturer,vendor,modelNumber,serialNumber,description,circuitId,assetNumber,
operatingSystem,rack,slot,port,region,division,department,address1,address2,city,state,zip,
building,floor,room,vendorPhone,vendorFax,userLastModified,lastModifiedDate,dateInstalled,lease,
leaseExpires,supportPhone,maintContract,vendorAssetNumber,maintContractExpires,displayCategory,
notifyCategory,pollerCategory,thresholdCategory,comment,username,password,enable,connection,autoenable,cpu,ram) 
values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

also include the fields in the statment string

            stmt.setString(46, asset.cpu);
            stmt.setString(47, asset.ram);

Sort of the same thing again further down the file. Prepared statement:

PreparedStatement stmt = conn.prepareStatement("UPDATE ASSETS SET
category=?,manufacturer=?,vendor=?,modelNumber=?,serialNumber=?,description=?,circuitId=?,assetNumber=?,
operatingSystem=?,rack=?,slot=?,port=?,region=?,division=?,department=?,address1=?,address2=?,city=?,state=?,
zip=?,building=?,floor=?,room=?,vendorPhone=?,vendorFax=?,userLastModified=?,lastModifiedDate=?,dateInstalled=?,
lease=?,leaseExpires=?,supportPhone=?,maintContract=?,vendorAssetNumber=?,maintContractExpires=?,displayCategory=?,
notifyCategory=?,pollerCategory=?,thresholdCategory=?,comment=?, username=?, password=?,enable=?,connection=?,
autoenable=?,cpu=?,ram=? WHERE nodeid=?");

Be careful in the next step. You have to insert your fields above the "stmt.setInt(...asset.nodeId)" and keep the numbers in order. So if the nodeId line has number 45, make your new fields 45 and 46 and change the nodeId line's number to 47.

brefore
            stmt.setInt(45, asset.nodeId);
after
            stmt.setString(45, asset.cpu);
            stmt.setString(46, asset.ram);
            stmt.setInt(47, asset.nodeId);

Add your fields to the asset list:

            asset.setCpu(rs.getString("cpu"));
            asset.setRam(rs.getString("ram"));

and also to the hardcoded list of human readable names.

        new String[] { "Cpu", "cpu" },        
        new String[] { "Ram", "ram" }  

opennms-webapp/src/main/java/org/opennms/web/asset/ModifyAssetServlet.java

Just one piece here: add your fields to the list of assets.

        asset.setCpu(getRequestParameter(request, "cpu"));
        asset.setRam(getRequestParameter(request, "ram"));

opennms-webapp/src/main/webapp/asset/modify.jsp

Add html code to the web page in order to show your fields and make them configurable. Where you put this within the form and what you name it is basically up to you.

          <tr>
            <td colspan="3"><h3>Hardware Data</h3></td>
          </tr>
          <tr>
            <td>Cpu</td>
            <td><input type="text" name="cpu" value="<%=asset.getCpu()%>" size="20" maxlength="32"/></td>
            <td>Ram</td>
            <td><input type="text" name="ram" value="<%=asset.getRam()%>" size="20" maxlength="32"/></td>
          </tr>

opennms-base-assembly/src/main/filtered/etc/create.sql

Configure column definitions for your fields in the database

        cpu             varchar(32),
        ram             varchar(10),

Build and run

Now, you can build OpenNMS and use your new assets.