Solaris WBEM Services の管理

API によるロギングの有効化

ログファイルの内容は現在 Log Viewer で表示できますが、独自の方法でログファイルを表示したい場合はロギング API を使用して Log Viewer を作成できます。この API を使用すると、次のことができます。

ログファイルへのデータの書き込み

アプリケーションでデータをログファイルに書き込むには、次の作業を行います。

データを書き込むために Solaris_LogRecord のインスタンスを作成する方法
  1. 必要なすべての java クラスをインポートします。例 5-1 に示すクラスは必要な最小限のクラスです。


    例 5-1 クラスのインポート

    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.*; 

  2. 公開クラス CreateLog と次の値を宣言します。

    • CIMClient の値

    • CIMObjectPath の値

    • CIMNameSpace の値


    例 5-2 CreateLog クラスと値の宣言

    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]);

  3. プロパティのベクトルが返されるように指定します。修飾子のプロパティに値を設定します。


    例 5-3 プロパティのベクトルと値の指定

    Vector keys = new Vector();  		
    CIMProperty 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); 

  4. ログ記録用に CIMObjectPath の新しいインスタンスを宣言します。


    例 5-4 CIMObjectPath の新しいインスタンスの宣言

    CIMObjectPath logreccop = new CIMObjectPath("Solaris_LogRecord", keys);

  5. Solaris_LogRecord の新しいインスタンスを宣言します。ファイルに書き込むようにプロパティのベクトルを設定します。


    例 5-5 インスタンスとプロパティの設定

    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();
    	}

  6. データがログファイルに書きこまれたあと、セッションを閉じます


    例 5-6 セッションを閉じる

    	// セッションを閉じる。
    	if(cc != null) {
    	    cc.close();
    	}
        }
    }

ログファイルからのデータの読み取り

アプリケーションでログファイルから Log Viewer にデータを読み込むには、次の作業を行います

Solaris_LogRecord のインスタンスを取得し、データを読み取る方法
  1. 必要なすべての java クラスをインポートします。例 5-7 に示すクラスは、インポートする必要がある最小限のクラスです。


    例 5-7 クラスのインポート

    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;

  2. クラス ReadLog を宣言します。


    例 5-8 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);

  3. ReadLog クラスのクライアント、オブジェクトパス、およびネームスペースの値を設定します。


    例 5-9 Solaris ログ記録の作成

    	} 	
    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"); 

  4. Solaris_LogRecord のインスタンスを列挙します。


    例 5-10 インスタンスの列挙

     Enumeration e = cc.enumInstances(cop, true);
    		for (; e.hasMoreElements(); ) {

  5. プロパティの値を出力デバイスに送ります。


    例 5-11 プロパティ値の送信

    	
    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("-----------------------------
          ----");
    
    		}

  6. エラーが発生した場合は、ユーザーにエラーメッセージを返します。


    例 5-12 エラーメッセージを返す

    ...
    }
    	catch (Exception e) {
    	    System.out.println("Exception: "+e);
    		e.printStackTrace();
    	}
    ...

  7. ファイルからデータが読み取られたあとセッションを閉じます。


    例 5-13 セッションを閉じる

    	// セッションを閉じる。
    	if(cc != null) {
    	    cc.close();
    	}
    }
    }

ロギングプロパティの設定

Solaris_LogServiceProperties クラスのインスタンスを作成し、そのプロパティ値を設定すれば、アプリケーションやプロバイダでロギングをどのように扱うかを制御できます。次の例は、ロギングプロパティの設定方法を示したものです。プロパティは、/usr/sadm/bin/wbem/wbemservices.properties ファイルに格納されます。


例 5-14 ロギングプロパティの設定

public class SetProps {
    public static void main(String args[]) throws CIMException {
	
	if ( args.length != 3) {
	    System.out.println("Usage: SetProps host username password"); 
	    System.exit(1);
	}

	CIMClient cc = null;
	try {
	    CIMNameSpace cns = new CIMNameSpace(args[0]);
	    cc = new CIMClient(cns, args[1], args[2]);

	 	CIMObjectPath logpropcop = new CIMObjectPath("Solaris_Log
   ServiceProperties");
	    Enumeration e = cc.enumInstances(logpropcop, true);
		for (; e.hasMoreElements(); ) {
			CIMObjectPath op = (CIMObjectPath)e.nextElement();
			CIMInstance ci = cc.getInstance(op);
			ci.setProperty("Directory", new CIMValue("/tmp/bar1/"));
			ci.setProperty("FileSize", new CIMValue("10"));
			ci.setProperty("NumFiles", new CIMValue("2"));
			ci.setProperty("SyslogSwitch", new CIMValue("off"));
			cc.setInstance(logpropcop,ci);
		}
	}
	catch (Exception e) {
	    System.out.println("Exception: "+e);
		e.printStackTrace();
	}

	// セッションを閉じる。
	if(cc != null) {
	    cc.close();
	}    
}