Red Hat Training

A Red Hat training course is available for Red Hat JBoss Operations Network

12.6. operations

12.6.1. リソースの起動および停止

リソースは、操作を使用して簡単に開始できます。
この例では、特定のリソースを名前で検索し、start() 関数を実行します。

例12.16 簡易起動

//find the resource
criteria = new ResourceCriteria();
criteria.addFilterName('My JBossAS')

var servers = ResourceManager.findResourcesByCriteria(criteria);
var myJBossAS = ProxyFactory.getResource(servers.get(0).id)
myJBossAS.start()
リソースタイプごとに独自の定義操作があります。また、start や stop などの単純なタスクであっても、リソースによっては異なる方法を持つ場合があります。プロキシーリソースの使用を試み、利用可能な操作を一覧表示する operations 方法を試みます。
rhqadmin@localhost:7080$ server.operations
Array of org.rhq.bindings.client.ResourceClientProxy$Operation
name     description
-----------------------------------------------------------------------
restart  Shutdown and then start this application server.
start    Start this application server. 
shutdown Shutdown this application server via script or JMX.
3 rows
より複雑な起動または停止スクリプトを使用して、同じタイプのリソースの配列を繰り返し処理できます。

例12.17 アレイ 1 の起動

//find the resources
//use a plugin filter to make sure they are all of the same type
criteria = new ResourceCriteria();
criteria.addFilterPluginName('JBossAS5')

var resources = ResourceManager.findResourcesByCriteria(criteria).toArray();
var resType = ResourceTypeManager.getResourceTypeByNameAndPlugin('JBossAS Server', 'JBossAS5');

// go through the array
var idx=0;
var jbossServers = new Array();

for( i in resources ) {
     if( resources[i].resourceType.id == resType.id ) {
          jbossServers[idx] = resources[i];
          idx = idx + 1;     
     }
}

// restart the resources
for( a in resources ) {
     var jboss = ProxyFactory.getResource(jbossServers[a].id);
     jboss.restart()
}

例12.18 アレイ 2 の起動

//find the resources
//use a plugin filter to make sure they are all of the same type
criteria = new ResourceCriteria();
criteria.addFilterPluginName('JBossAS5')
criteria.addFilterResourceTypeName('JBossAS Server');

var jbossServers = ResourceManager.findResourcesByCriteria(criteria).toArray();

// restart the resources
for( a in jbossServers ) {
     var jboss = ProxyFactory.getResource(jbossServers[a].id);
     jboss.restart()
}

12.6.2. スケジューリング操作

最も簡単な方法として、リソースのプロキシーを作成し、そのプロキシーで proxyName.operationName() の形式で操作を実行します。

例12.19 即時操作

rhqadmin@localhost:7080$ var agent = ProxyFactory.getResource(10008)
rhqadmin@localhost:7080$ agent.executeAvailabilityScan(true)
Invoking operation executeAvailabilityScan
Configuration [13903] - null
  isChangesOnly = true
  agentName = server.example.com
  resourceAvailabilities [0] {
  }
操作はスケジュールで実行できます。スケジュールには、いくつかの設定項目が必要です。
  • リソース ID
  • 操作名
  • 遅延期間: 今後操作を開始するタイミング(任意)
  • 繰り返し間隔とカウント(オプション)
  • タイムアウト期間(任意)
  • 設定パラメーター(操作で必要な場合)
  • スケジュールされた操作の説明(任意)
この例では、特定のエージェントで可用性スキャンを実行します。

例12.20 スケジュールされた操作の例

// find the agent
var rc = ResourceCriteria();
rc.addFilterResourceTypeName("RHQ Agent");
rc.addFilterVersion("3.3");

var agent = ResourceManager.findResourcesByCriteria(rc);

//set the config properties for the operation
var config = new Configuration();
config.put(new PropertySimple("changesOnly", "true") );

//schedule the operation
OperationManager.scheduleResourceOperation(
          agent.get(0).id,
          "executeAvailabilityScan",
	  0, // 0 means that the delay was skipped
          1,
	  0, // this skips the repeat count
          10000000,
	  config, 
          "test from cli"
	  );
これにより、スケジュールされた操作の情報を即座に出力します。
ResourceOperationSchedule:
        resource: Resource[id=10008, uuid=e11390ec-34c4-49df-a4b6-c37c516f545c, type={RHQAgent}RHQ Agent, key=server.example.com RHQ Agent, name=RHQ Agent, parent=server.example.com, version=3.3]

12.6.3. 操作の結果の取得

一部の操作では成功または失敗のみが報告され、これだけが必要になります。その他の操作は、リソースから情報を取得したり、リソースの変更をより複雑な変更したりすることができます。その場合は、他のタスクで使用できるように、その情報を返す必要があります。
The fetchResults(true) メソッドを使用すると、操作履歴の検索の一部として操作の結果を返すことができます。
// find the resource
criteria = new ResourceCriteria(); 
criteria.addFilterResourceTypeName('Linux');
criteria.addFilterName('server.example.com');
ResourceManager.findResourcesByCriteria(criteria);

var resource = ResourceManager.findResourcesByCriteria(criteria);

// search for the operation history
var opcrit = ResourceOperationHistoryCriteria();
// get the operation for the resource ID
opcrit.addFilterResourceIds(resource.get(0).id);
// filter by the operation name
opcrit.addFilterOperationName("viewProcessList")
opcrit.fetchResults(true);

// get the data and print the results
var r = OperationManager.findResourceOperationHistoriesByCriteria(opcrit)
var h = r.get(0);
var c = h.getResults();
pretty.print(c)

例12.21 プロセススキャンの結果の出力

この例では、スクリプトが最初に操作(プラットフォーム上でのプロセススキャン)を実行し、結果を出力します。
スクリプトの最初の部分は、リソース ID の要件を設定し、そのリソースを検索します。
if (args.length != 1) {
    throw "we need a resource id as an argument";
}

var platform = ResourceManager.getResource(args[0]);
次の部分は、プロセスのスキャン操作をスケジュールします。の説明にあるように 「スケジューリング操作」、リソース ID、操作名、および操作設定が設定されます。
var ros = OperationManager.scheduleResourceOperation(
   platform.id,
   "viewProcessList",
   0,
   1,
   0,
   15,
   null,
   "test operation"
);
最後の部分は、2 つの検索設定で操作履歴を取得します。
  • fetchResults(true)。ステータスだけでなく、操作結果データを含めるために必要です。
  • この場合、ソートメソッド addSortStartTime
スクリプトには、スクリプトが結果の取得を試みる前に操作の完了時間を確保するためにもいくつかのスリープがあります。
var opcrit = ResourceOperationHistoryCriteria();
opcrit.addFilterResourceIds(platform.id);
opcrit.fetchResults(true); // request the additional optional data in the result
opcrit.addSortStartTime(PageOrdering.DESC); // sort by start time
java.lang.Thread.sleep(1000); // wait a second to make sure operation is in the history

// wait for up to 15 seconds for last operation to complete, then print result
now=new Date().getTime();
while (new Date().getTime() - now < 15000 ) {
   operations = OperationManager.findResourceOperationHistoriesByCriteria(opcrit);
   if (operations.get(0).getResults() == null) {
      println("operation still pending result");
      java.lang.Thread.sleep(1000);
   } else {
      pretty.print(operations.get(0).getResults());
      break;
   }
}
if (operations.get(0).getErrorMessage() != null) {
      println("Error getting process list: ");
      pretty.print(operations.get(0).getErrorMessage());
}
このスクリプトは、操作が正常に完了すれば、操作の結果を出力します。
   } else {
      pretty.print(operations.get(0).getResults());
      break;
   }

12.6.4. リソースの動作履歴の確認

リソースの操作履歴はオブジェクトとして存在するため、他のオブジェクトと同じ基準で検索できます。

例12.22 操作履歴の表示

// find the resource
var rc = ResourceCriteria();
rc.addFilterPluginName("RHQAgent");
rc.addFilterName("RHQ Agent");
rc.addFilterResourceTypeName("RHQ Agent");
rc.addFilterDescription("Agent");

var agent = ResourceManager.findResourcesByCriteria(rc);

// print the operation history for the resource
var opcrit = ResourceOperationHistoryCriteria()
opcrit.addFilterResourceIds(agent.get(0).id)
OperationManager.findResourceOperationHistoriesByCriteria(opcrit);
(successful)操作の結果は、results テーブルの Configuration オブジェクトに置かれます。
resource                                             results
-----------------------------------------------------------------------------------------------------------------
Resource[id=10008, uuid=e11390ec-34c4-49df-a4b6-c37c516f545c, type={RHQAgent}RHQ Agent, key=server.example.com RHQ Age Configuration[id=13903]
Resource[id=10008, uuid=e11390ec-34c4-49df-a4b6-c37c516f545c, type={RHQAgent}RHQ Agent, key=server.example.com RHQ Age Configuration[id=13913]
2 rows