Currently, you can view log file content in CIM Workshop. However, you can develop your own log viewer if you prefer to view log files in a customized manner. You can use the logging application programming interfaces (APIs) to develop a log viewer. The APIs enable you to write data from an application to a log file or read data from a log file to your log viewer.
Enabling an application to write data to a log file involves the following main tasks:
Creating a new instance of the Solaris_LogRecord class
Specifying the properties that will be written to the log file and setting values for the property qualifiers
Setting the new instance and properties to print
Import all necessary java.rmi classes.
import java.rmi.*; import com.sun.wbem.client.CIMClient; import com.sun.wbem.cim.CIMInstance; import com.sun.wbem.cim.CIMValue; import com.sun.wbem.cim.CIMProperty; import com.sun.wbem.cim.CIMNameSpace; import com.sun.wbem.cim.CIMObjectPath; import com.sun.wbem.cim.CIMClass; import com.sun.wbem.cim.CIMException; import com.sun.wbem.solarisprovider.*; import java.util.*; import java.util.Enumeration; |
Declare the public class CreateLog and the following values:
CIMClient value
CIMObjectPath value
CIMNameSpace value
public class CreateLog { public static void main(String args[]) throws CIMException { if ( args.length != 3) { System.out.println("Usage: CreateLog host username password"); System.exit(1); } CIMClient cc = null; CIMObjectPath cop = null; try { CIMNameSpace cns = new CIMNameSpace(args[0]); cc = new CIMClient(cns, args[1], args[2]); |
Specify the vector of properties to be returned. Set values for the properties of the qualifiers.
Vector keys = new Vector(); CIMProperty logsvcKey = new CIMProperty("RecordID"); logsvcKey.setValue(new CIMValue(new Integer(0))); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("RecordHashCode"); logsvcKey.setValue(new CIMValue(new Integer(0))); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("Filename"); logsvcKey.setValue(new CIMValue("some_file")); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("category"); logsvcKey.setValue(new CIMValue(new Integer(2))); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("severity"); logsvcKey.setValue(new CIMValue(new Integer(2))); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("AppName"); logsvcKey.setValue(new CIMValue("SomeApp")); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("UserName"); logsvcKey.setValue(new CIMValue("molly")); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("ClientMachineName"); logsvcKey.setValue(new CIMValue("dragonfly")); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("ServerMachineName"); logsvcKey.setValue(new CIMValue("spider")); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("SummaryMessage"); logsvcKey.setValue(new CIMValue("brief_description")); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("DetailedMessage"); logsvcKey.setValue(new CIMValue("detailed_description")); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("data"); logsvcKey.setValue(new CIMValue("0xfe 0x45 0xae 0xda")); keys.addElement(logsvcKey); logsvcKey = new CIMProperty("SyslogFlag"); logsvcKey.setValue(new CIMValue(new Boolean(true))); keys.addElement(logsvcKey); |
Declare the new instance of the CIMObject Path for the log record.
CIMObjectPath logreccop = new CIMObjectPath("Solaris_LogRecord", keys); |
Declare the new instance of Solaris_LogRecord. Set vector of properties to write to a file.
CIMInstance ci = new CIMInstance(); ci.setClassName("Solaris_LogRecord"); ci.setProperties(keys); //System.out.println(ci.toString()); cc.setInstance(logreccop,ci); } catch (Exception e) { System.out.println("Exception: "+e); e.printStackTrace(); } |
Close the session after data has been written to the log file.
// close session. if(cc != null) { cc.close(); } } } |
Enabling an application to read data from a log file to a log viewer involves the following tasks:
Enumerating instances of the Solaris_LogRecord class
Getting the desired instance
Printing properties of the instance to an output device, typically a user interface
Import all necessary java.rmi classes.
import java.rmi.*; import com.sun.wbem.client.CIMClient; import com.sun.wbem.cim.CIMInstance; import com.sun.wbem.cim.CIMValue; import com.sun.wbem.cim.CIMProperty; import com.sun.wbem.cim.CIMNameSpace; import com.sun.wbem.cim.CIMObjectPath; import com.sun.wbem.cim.CIMClass; import com.sun.wbem.cim.CIMException; import com.sun.wbem.solarisprovider.*; import java.util.*; import java.util.Enumeration; |
Declare the class ReadLog.
public class ReadLog { public static void main(String args[]) throws CIMException { if ( args.length != 3) { System.out.println("Usage: ReadLog host username password"); System.exit(1); |
Set client, object path, and namespace values of the ReadLog class.
} CIMClient cc = null; CIMObjectPath cop = null; try { CIMNameSpace cns = new CIMNameSpace(args[0]); cc = new CIMClient(cns, args[1], args[2]); cop = new CIMObjectPath("Solaris_LogRecord"); |
Enumerate instances of Solaris_LogRecord.
Enumeration e = cc.enumInstances(cop, true); for (; e.hasMoreElements(); ) { |
Send property values to an output device.
System.out.println("------------------------ ---------"); CIMObjectPath op = (CIMObjectPath)e.nextElement(); CIMInstance ci = cc.getInstance(op); System.out.println("Record ID : " + (((Long)ci.getProperty("RecordID").getValue(). getValue()).longValue())); System.out.println("Log filename : " + ((String)ci.getProperty("FileName").getValue(). getValue())); int categ = (((Integer)ci.getProperty("category"). getValue().getValue()).intValue()); if (categ == 0) System.out.println("Category : Application Log"); else if (categ == 1) System.out.println("Category : Security Log"); else if (categ == 2) System.out.println("Category : System Log"); int severity = (((Integer)ci.getProperty ("severity").getValue().getValue()).intValue()); if (severity == 0) System.out.println("Severity : Informational"); else if (severity == 1) System.out.println("Severity : Warning Log!"); else if (severity == 2) System.out.println("Severity : Error!!"); System.out.println("Log Record written by :" + ((String)ci.getProperty("AppName").getValue(). getValue())); System.out.println("User : " + ((String)ci. getProperty("UserName").getValue().getValue())); System.out.println("Client Machine : " + ((String)ci. getProperty("ClientMachineName").getValue().getValue ())); System.out.println("Server Machine : " + ((String)ci. getProperty("ServerMachineName").getValue().getValue ())); System.out.println("Summary Message : " + ((String) ci.getProperty("SummaryMessage").getValue().getValue ())); System.out.println("Detailed Message : " + ((String) ci.getProperty("DetailedMessage").getValue().getValue ())); System.out.println("Additional data : " + ((String) ci.getProperty("data").getValue().getValue())); boolean syslogflag = ((Boolean)ci.getProperty("syslogflag").getValue(). getValue()).booleanValue(); if (syslogflag == true) { System.out.println("Record was written to syslog as well"); } else { System.out.println("Record was not written to syslog"); } System.out.println("----------------------------- ----"); } |
Return an error message to the user if an error condition occurs.
} catch (Exception e) { System.out.println("Exception: "+e); e.printStackTrace(); } |
Close the session when the data has been read from the file.
// close session. if(cc != null) { cc.close(); } } } |