@Override public InputSplit[] getSplits(JobConf conf, int numberOfSplits) throws IOException { SplunkConf splunkConf = new SplunkConf(conf); Service mainService = SplunkService.connect(splunkConf); List<Indexer> indexers = IndexerProvider.getIndexers(SplunkService.connect(splunkConf)); Indexer mainIndexer = new Indexer(mainService.getHost(), mainService.getPort()); indexers.add(0, mainIndexer); InputSplit[] splits = new InputSplit[indexers.size()]; for (int i = 0; i < indexers.size(); i++) { Indexer indexer = indexers.get(i); Service service = SplunkService.connect(splunkConf, indexer.getHost(), indexer.getPort()); SplunkJob splunkJob = SplunkJob.createSplunkJob(service, splunkConf); int start = 0; // Add one for the header int end = splunkJob.getNumberOfResultsFromJob(splunkConf) + 1; boolean skipHeader = i > 0; splits[i] = new IndexerSplit(indexer, splunkJob.getJob().getSid(), start, end, skipHeader); } return splits; }
/* (non-Javadoc) * @see org.springframework.integration.splunk.core.IService#isOpen() */ public boolean isOpen() { boolean result = true; try { service.getApplications(); } catch (Throwable t) { result = false; } return result; }
static void run(String[] args) throws IOException { Command command = Command.splunk("test").parse(args); Service service = Service.connect(command.opts); String path = command.args.length > 0 ? command.args[0] : "/"; ResponseMessage response = service.get(path); int status = response.getStatus(); System.out.println(String.format("=> %d", status)); if (status != 200) return; BufferedReader reader = new BufferedReader(new InputStreamReader(response.getContent(), "UTF8")); while (true) { String line = reader.readLine(); if (line == null) break; System.out.println(line); } }
public SplunkConnection(SplunkServer splunkServer) { Map<String, Object> args = new HashMap<String, Object>(); if (splunkServer.getHost() != null) { args.put("host", splunkServer.getHost()); } if (splunkServer.getPort() != 0) { args.put("port", splunkServer.getPort()); } if (splunkServer.getScheme() != null) { args.put("scheme", splunkServer.getScheme()); } if (splunkServer.getApp() != null) { args.put("app", splunkServer.getApp()); } if (splunkServer.getOwner() != null) { args.put("owner", splunkServer.getOwner()); } args.put("username", splunkServer.getUserName()); args.put("password", splunkServer.getPassword()); service = Service.connect(args); }
ServiceNode(Service service) { super(service.getInfo(), new ServiceKids(service)); setDisplayName(service.getInfo().getServerName()); }
/* (non-Javadoc) * @see org.springframework.integration.splunk.core.IService#close() */ public void close() { service.logout(); }
@Override public void runJob() { addLog("开始更新基线", LOG_TYPE_SUCCESS); Calendar cl = Calendar.getInstance(); long times = cl.getTimeInMillis(); TaskInfoDto taskInfo = (TaskInfoDto) this.params.get(TASKINFO_KEY); String[] strs = taskInfo.getConfigDesc().split(";"); // splunk服务器连接信息 String[] serCfg = strs[0].split(","); if (serCfg.length != 4) { addLog("连接服务器参数不对:" + strs[0], LOG_TYPE_ERROR); return; } // 基本查询条件 String baseStr = strs[2]; String baseStr2 = strs.length < 14 ? "" : strs[13]; // 统计值字段名 String cntCol = strs[3]; // 时间字段名 String timeCol = strs[4]; try { // 连接参数 ServiceArgs loginArgs = new ServiceArgs(); loginArgs.setUsername(serCfg[2]); loginArgs.setPassword(serCfg[3]); loginArgs.setHost(serCfg[0]); loginArgs.setPort(NumberUtils.toInt(serCfg[1])); // 连接 Service service = Service.connect(loginArgs); // 延迟 int delayMi = NumberUtils.toInt(strs[5], 0); String searchStr = createNSearchStr(baseStr, cntCol, timeCol, delayMi, baseStr2); this.addLog("查询:" + searchStr, LOG_TYPE_SUCCESS); // 查询 Job nJob = service.search(searchStr); // 最近1分钟 ResultsReader results; Event et; Iterator<Event> ie; InputStream is; ////////////////////////////////////////////////////////// 最近1分钟 while (!nJob.isDone()) { Thread.sleep(1000); } is = nJob.getResults(new Args("output_mode", "json")); results = createResultsReader(ResultsReaderJson.class, is); List<String> nNumVal1 = new ArrayList<String>(); List<String> nNumVal2 = new ArrayList<String>(); List<String> nNumVal3 = new ArrayList<String>(); List<Date> nDate = new ArrayList<Date>(); if (nJob.getResultCount() > 0) { ie = results.iterator(); while (ie.hasNext()) { if (strs.length > 12 && "1".equals(strs[12])) { et = ie.next(); nNumVal1.add(et.get("my_cnt1")); nNumVal2.add(et.get("my_cnt2")); nDate.add(convertDate(et.get("my_time"))); } else if (strs.length > 12 && "2".equals(strs[12])) { while (ie.hasNext()) { et = ie.next(); nNumVal1.add(et.get("my_cnt1")); nNumVal2.add(et.get("my_cnt2")); nNumVal3.add(et.get("my_cnt3")); nDate.add(convertDate(et.get("my_time"))); } } else { et = ie.next(); nNumVal1.add(et.get("my_cnt1")); nDate.add(convertDate(et.get("my_time"))); } } } is.close(); nJob.cancel(); ////////////////////////////////////////////////////////// 最近1分钟 end String curTime; BizMiDto dto; BizMiDao bdao = new BizMiDao(); double rr; if (!nNumVal1.isEmpty()) { for (int i = 0; i < nNumVal1.size(); i++) { cl.set(Calendar.SECOND, 0); if (nDate.get(i) != null) { cl.setTime(nDate.get(i)); curTime = DateUtil.datetimeFormat.format(nDate.get(i)); } else { cl.add(Calendar.MINUTE, -delayMi); curTime = DateUtil.datetimeFormat.format(cl.getTime()); } try { dto = bdao.findByTimeAndMi(strs[7], strs[8], curTime, strs[6]); if (dto == null) { this.addLog( "更新出错,找不到更新点:" + strs[7] + "::" + strs[8] + "::" + curTime + "::" + strs[6], LOG_TYPE_ERROR); this.addLog("更新出错,找不到更新点", LOG_TYPE_ERROR); } dto.setVal(ckVal(nNumVal1.get(i))); bdao.updateByTimeAndMi(dto); } catch (Exception e) { addLog("更新入口VAL:" + formatExpection(e), LOG_TYPE_ERROR); } if (!nNumVal2.isEmpty()) { try { dto = bdao.findByTimeAndMi(strs[7], strs[9], curTime, strs[6]); if (dto == null) { this.addLog( "更新出错,找不到更新点:" + strs[7] + "::" + strs[9] + "::" + curTime + "::" + strs[6], LOG_TYPE_ERROR); } dto.setVal(ckVal(nNumVal2.get(i))); bdao.updateByTimeAndMi(dto); } catch (Exception e) { addLog("更新出口VAL:" + formatExpection(e), LOG_TYPE_ERROR); } try { dto = bdao.findByTimeAndMi(strs[7], strs[10], curTime, strs[6]); if (dto == null) { this.addLog( "更新出错,找不到更新点:" + strs[7] + "::" + strs[10] + "::" + curTime + "::" + strs[6], LOG_TYPE_ERROR); } rr = NumberUtils.toDouble(nNumVal2.get(i), 0) / NumberUtils.toDouble(nNumVal1.get(i), 0); if (rr == Double.NaN) { dto.setVal(String.valueOf(0)); } else { dto.setVal(String.valueOf(rr)); } bdao.updateByTimeAndMi(dto); } catch (Exception e) { addLog("更新响应率:" + formatExpection(e), LOG_TYPE_ERROR); } } if (!nNumVal3.isEmpty()) { dto = bdao.findByTimeAndMi(strs[7], strs[11], curTime, strs[6]); dto.setVal(ckVal(nNumVal3.get(i))); bdao.updateByTimeAndMi(dto); try { bdao.saveByTable(dto, strs[6]); } catch (Exception e) { addLog("更新响应时间:" + formatExpection(e), LOG_TYPE_ERROR); } } } } addLog("任务耗时:" + (Calendar.getInstance().getTimeInMillis() - times) + "毫秒", LOG_TYPE_SUCCESS); } catch (Exception e) { e.printStackTrace(); addLog(formatExpection(e), LOG_TYPE_ERROR); } }