Groovy - 如何设置参数外部命令
我有一个软 HP BSM。 操作员的操作将用 Groovy 脚本编写。
这是示例:
import java.util.List;
import com.hp.opr.api.scripting.Action;
import com.hp.opr.api.scripting.Event;
import com.hp.opr.api.scripting.EventActionFlag;
import com.hp.opr.api.scripting.LifecycleState;
import com.hp.opr.api.scripting.MatchInfo;
import com.hp.opr.api.scripting.NodeInfo;
import com.hp.opr.api.scripting.PolicyType;
import com.hp.opr.api.scripting.Priority;
import com.hp.opr.api.scripting.ResolutionHints;
import com.hp.opr.api.scripting.Severity;
/*
* This example set all possible event attribute to some example values.
*/
class SimpleExample_new
{
def init()
{
}
def destroy()
{
}
def process(List<Event> events)
{
events.each {
event -> modifyEvent(event);
"cmd.exe /c C:\\test\\sd_event.exe -f C:\\test\\sd_event.ini -v event_id=2 description='$description' status=Registered priority=low".execute().text
}
}
def modifyEvent(Event event)
{
// "cmd.exe /c C:\\test\\sd_event.exe -f C:\\test\\sd_event.ini -v event_id=10 description='$description' status=Registered priority=low".execute().text
String application = event.getApplication();
event.setApplication("Modified by ostap: " + application);
String description = event.getDescription();
event.setDescription("Modified by ostap: " + description);
long groupId = event.getAssignedGroupId();
event.setAssignedGroupId(groupId);
int assignedUserId = event.getAssignedUserId();
event.setAssignedUserId(assignedUserId);
String category = event.getCategory();
event.setCategory("Modified by EPI: " + category);
String correlationKeyPattern = event.getCloseKeyPattern();
event.setCloseKeyPattern("Modified by EPI: " + correlationKeyPattern);
String etiInfo = event.getEtiHint();
event.setEtiHint(etiInfo);
String correlationKey = event.getKey();
event.setKey("Modified by EPI: " + correlationKey);
MatchInfo matchInfo = createSampleMatchInfo();
event.setMatchInfo(matchInfo);
event.setNoDedup(true);
ResolutionHints hints = createSampleResolutionHints();
event.setNodeHints(hints);
String object = event.getObject();
event.setObject("Modified by EPI: " + object);
String omServiceId = event.getOmServiceId();
event.setOmServiceId(omServiceId);
String omUser = event.getOmUser();
event.setOmUser(omUser);
String originalText = event.getOriginalData();
event.setOriginalData("Modified by EPI: " + originalText);
String originalId = event.getOriginalId();
event.setOriginalId(originalId);
event.setSeverity(Severity.MINOR);
String solution = event.getSolution();
event.setSolution("Modified by ostap: " + solution);
ResolutionHints sourceCiHints = createSampleResolutionHints();
event.setSourceCiHints(sourceCiHints);
event.setState(LifecycleState.IN_PROGRESS);
String subCategory = event.getSubCategory();
event.setSubCategory("Modified by EPI: " + subCategory);
event.setTimeReceived(new Date());
String title = event.getTitle();
event.setTitle("Modified by EPI: " + title);
String type = event.getType();
event.setType("Modified by EPI: " + type);
}
def ResolutionHints createSampleResolutionHints()
{
ResolutionHints hints = new ResolutionHints(false);
hints.setCoreId("CoreId");
hints.setDnsName("mydqdn.com");
hints.setHint("My Hint");
hints.setIpAddress("0.0.0.0");
return hints;
}
def MatchInfo createSampleMatchInfo()
{
MatchInfo matchInfo = new MatchInfo(false);
matchInfo.setConditionId("conditionId");
matchInfo.setPolicyName("policyName");
matchInfo.setPolicyType(PolicyType.CONSOLE);
return matchInfo;
}
}
我想修改示例。
我想要获取“描述”并将此“描述”设置(放置)到外部命令(sd_event.exe)
cmd.exe /c C:\\test\\sd_event.exe -f C:\\test\\sd_event.ini -v event_id=50 description=____ status=Registered priority=low".execute().text
我也尝试运行:
cmd.exe /c C:\\test\\mybatch.bat".execute().text
但我不知道如何将参数放入bat文件
mybatch.bat:
C:\test\sd_event.exe -f sd_event.ini -v event_id=5 description=%1 status="Registered" priority=low
I have a soft HP BSM.
Actions for operators will write in Groovy script.
this is example:
import java.util.List;
import com.hp.opr.api.scripting.Action;
import com.hp.opr.api.scripting.Event;
import com.hp.opr.api.scripting.EventActionFlag;
import com.hp.opr.api.scripting.LifecycleState;
import com.hp.opr.api.scripting.MatchInfo;
import com.hp.opr.api.scripting.NodeInfo;
import com.hp.opr.api.scripting.PolicyType;
import com.hp.opr.api.scripting.Priority;
import com.hp.opr.api.scripting.ResolutionHints;
import com.hp.opr.api.scripting.Severity;
/*
* This example set all possible event attribute to some example values.
*/
class SimpleExample_new
{
def init()
{
}
def destroy()
{
}
def process(List<Event> events)
{
events.each {
event -> modifyEvent(event);
"cmd.exe /c C:\\test\\sd_event.exe -f C:\\test\\sd_event.ini -v event_id=2 description='$description' status=Registered priority=low".execute().text
}
}
def modifyEvent(Event event)
{
// "cmd.exe /c C:\\test\\sd_event.exe -f C:\\test\\sd_event.ini -v event_id=10 description='$description' status=Registered priority=low".execute().text
String application = event.getApplication();
event.setApplication("Modified by ostap: " + application);
String description = event.getDescription();
event.setDescription("Modified by ostap: " + description);
long groupId = event.getAssignedGroupId();
event.setAssignedGroupId(groupId);
int assignedUserId = event.getAssignedUserId();
event.setAssignedUserId(assignedUserId);
String category = event.getCategory();
event.setCategory("Modified by EPI: " + category);
String correlationKeyPattern = event.getCloseKeyPattern();
event.setCloseKeyPattern("Modified by EPI: " + correlationKeyPattern);
String etiInfo = event.getEtiHint();
event.setEtiHint(etiInfo);
String correlationKey = event.getKey();
event.setKey("Modified by EPI: " + correlationKey);
MatchInfo matchInfo = createSampleMatchInfo();
event.setMatchInfo(matchInfo);
event.setNoDedup(true);
ResolutionHints hints = createSampleResolutionHints();
event.setNodeHints(hints);
String object = event.getObject();
event.setObject("Modified by EPI: " + object);
String omServiceId = event.getOmServiceId();
event.setOmServiceId(omServiceId);
String omUser = event.getOmUser();
event.setOmUser(omUser);
String originalText = event.getOriginalData();
event.setOriginalData("Modified by EPI: " + originalText);
String originalId = event.getOriginalId();
event.setOriginalId(originalId);
event.setSeverity(Severity.MINOR);
String solution = event.getSolution();
event.setSolution("Modified by ostap: " + solution);
ResolutionHints sourceCiHints = createSampleResolutionHints();
event.setSourceCiHints(sourceCiHints);
event.setState(LifecycleState.IN_PROGRESS);
String subCategory = event.getSubCategory();
event.setSubCategory("Modified by EPI: " + subCategory);
event.setTimeReceived(new Date());
String title = event.getTitle();
event.setTitle("Modified by EPI: " + title);
String type = event.getType();
event.setType("Modified by EPI: " + type);
}
def ResolutionHints createSampleResolutionHints()
{
ResolutionHints hints = new ResolutionHints(false);
hints.setCoreId("CoreId");
hints.setDnsName("mydqdn.com");
hints.setHint("My Hint");
hints.setIpAddress("0.0.0.0");
return hints;
}
def MatchInfo createSampleMatchInfo()
{
MatchInfo matchInfo = new MatchInfo(false);
matchInfo.setConditionId("conditionId");
matchInfo.setPolicyName("policyName");
matchInfo.setPolicyType(PolicyType.CONSOLE);
return matchInfo;
}
}
And I want modify example.
I want get "description" and set(put) this "description" to external command (sd_event.exe)
cmd.exe /c C:\\test\\sd_event.exe -f C:\\test\\sd_event.ini -v event_id=50 description=____ status=Registered priority=low".execute().text
I try also run:
cmd.exe /c C:\\test\\mybatch.bat".execute().text
but i don't know how to put parametres to bat file
mybatch.bat:
C:\test\sd_event.exe -f sd_event.ini -v event_id=5 description=%1 status="Registered" priority=low
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于代码中的格式和错误,很难看出发生了什么,或者您在问什么...
当您有:
什么是
事件
?我只能看到该函数中的事件列表...假设这只是剪切和粘贴的问题,我认为您正在寻找的答案是:
但是正如我所说,我不确定...您必须自己将该函数的输出写入
result.txt
,因为从 Java/Groovy 调用 shell 时无法以这种方式进行重定向Due to the formatting and errors in your code, it's hard to see what is going on, or what you are asking...
When you have:
What is
event
? I can only see aList
of events in that function...Assuming this is just a problem with cut and paste, I think the answer you are looking for is:
But as I said, I'm not sure... You will have to write the output of that function to
result.txt
yourself, as you cannot do redirection that way when calling the shell from Java/Groovy