| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 大专栏  Tomcat监控助手-自动重启相关服务">233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 
 | 
 
 
 
 public class TomcatMonitor {
 
 
 
 private JTextField textField1;
 
 
 
 private JTextField textField2;
 
 
 
 private JTextPane logPanel;
 
 
 
 private JButton helpButton;
 JPanel mainPanel;
 
 
 
 private JButton jkButton;
 
 
 
 private JTextField textField3;
 private JTabbedPane tp;
 private JTextField snatchNodeFd = new JTextField(60);
 private JTextField snatchUrlField = new JTextField(60);
 
 
 
 private boolean listenFlag = true;
 private final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 private String tomcatHome;
 private String listenUrl;
 private String interval;
 private String snatchNode;
 private String snatchItems;
 private String snatchUrl;
 
 
 
 private ListenThread lt = null;
 public TomcatMonitor() {
 initSecondPane();
 loadConf();
 helpButton.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 JOptionPane.showMessageDialog(null, "这是一个tomcat监控小工具,通过定时扫描指定的页面来确认服务是否正常。n在服务异常时,自动重新启动tomcat服务器。n启动是通过命令行启动的tomcat的startup.bat脚本。n", "使用说明", JOptionPane.INFORMATION_MESSAGE);
 }
 });
 logPanel.setContentType("text/html");
 logPanel.setEditable(false);
 jkButton.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 String text = "监控中...";
 if (jkButton.getText().equals(text)) {
 int r = JOptionPane.showConfirmDialog(null,"停止监控?","系统提示",JOptionPane.YES_NO_OPTION);
 if (r == JOptionPane.YES_OPTION) {
 TomcatMonitor.this.lt = null;
 TomcatMonitor.this.listenFlag = false;
 jkButton.setText("开始监控");
 }
 return;
 }
 boolean validateFlag = validateForm();
 if (validateFlag) {
 TomcatMonitor.this.listenFlag = true;
 if (lt == null ) {
 lt = new ListenThread();
 lt.start();
 }
 jkButton.setText(text);
 }
 }
 });
 insertLog("tomcat监控助手启动成功!");
 }
 
 
 
 private void initSecondPane() {
 JPanel secondPane = new JPanel();
 GridBagLayout gbl = new GridBagLayout();
 secondPane.setLayout(gbl);
 List<SnatchConfig> snatchConfigs = getSnatchConfigs();
 SnatchInfoTableModel sitm = new SnatchInfoTableModel(snatchConfigs);
 final JTable jt = new JTable(sitm);
 Font font = new Font("宋体",Font.BOLD,13);
 jt.getTableHeader().setFont(font);
 jt.setFont(font);
 JScrollPane jp = new JScrollPane(jt);
 
 for (int i=0;i<jt.getColumnCount();i++) {
 if (i == 0)
 jt.getColumnModel().getColumn(i).setPreferredWidth(30);
 if (i == 1)
 jt.getColumnModel().getColumn(i).setPreferredWidth(100);
 if (i == 2)
 jt.getColumnModel().getColumn(i).setPreferredWidth(200);
 }
 jt.setRowHeight(40);
 jt.setPreferredScrollableViewportSize(new Dimension(820,500));
 JLabel snatchNodeLbl = new JLabel("抓取项所属节点:");
 snatchNodeFd.setPreferredSize(new Dimension(200,30));
 JLabel snatchUrlLabel = new JLabel("开启抓取项URL:");
 JButton saveSnatchConfigBtn = new JButton("保存配置");
 snatchUrlField.setToolTipText("请输入抓取项URL");
 snatchUrlField.setPreferredSize(new Dimension(200,30));
 secondPane.add(jp,new GBC(0,0,2,1).setAnchor(GridBagConstraints.WEST).setInsets(-40,10,0,10));
 secondPane.add(snatchNodeLbl,new GBC(0,1,1,1).setAnchor(GridBagConstraints.WEST).setInsets(10,10,10,10));
 secondPane.add(snatchNodeFd,new GBC(1,1,2,1).setAnchor(GridBagConstraints.WEST).setInsets(10,10,10,10));
 secondPane.add(snatchUrlLabel,new GBC(0,3,1,1).setAnchor(GridBagConstraints.WEST).setInsets(10,10,10,10));
 secondPane.add(snatchUrlField,new GBC(1,3,2,1).setAnchor(GridBagConstraints.WEST).setInsets(10,10,10,10));
 secondPane.add(saveSnatchConfigBtn,new GBC(1,4,1,1).setAnchor(GridBagConstraints.CENTER));
 tp.add("抓取项配置",secondPane);
 
 
 
 saveSnatchConfigBtn.addActionListener(new AbstractAction() {
 @Override
 public void actionPerformed(ActionEvent e) {
 boolean dataChangeFlag = false;
 
 String snatchNode = snatchNodeFd.getText();
 if (!snatchNode.equals(TomcatMonitor.this.snatchNode) && !"".equals(snatchNode)) {
 dataChangeFlag = true;
 PropertiesUtils.setString(PropertiesUtils.KEY_SNATCH_NODE,snatchNode);
 TomcatMonitor.this.snatchNode = snatchNode;
 }
 
 String snatchURL = snatchUrlField.getText();
 if (!snatchURL.equals(snatchUrl) && !"".equals(snatchURL)) {
 dataChangeFlag = true;
 PropertiesUtils.setString(PropertiesUtils.KEY_SNATCH_URL,snatchURL);
 TomcatMonitor.this.snatchUrl = snatchURL;
 }
 int rows = jt.getRowCount();
 if (rows > 0) {
 StringBuffer snatchConfigBuff = new StringBuffer();
 for (int i=0;i<rows;i++) {
 String thirdSystem = (String) jt.getModel().getValueAt(i,1);
 String snatchItem = (String) jt.getModel().getValueAt(i,2);
 boolean startFlag = (Boolean)jt.getModel().getValueAt(i,3);
 snatchConfigBuff.append(thirdSystem).append("#").append(snatchItem).append("#").append(startFlag?1:0).append(",");
 }
 String snatchItems = snatchConfigBuff.substring(0,snatchConfigBuff.length()-1);
 if (!snatchItems.equals(TomcatMonitor.this.snatchItems)) {
 
 PropertiesUtils.setString(PropertiesUtils.KEY_SNATCH_ITEMS,snatchItems);
 dataChangeFlag = true;
 TomcatMonitor.this.snatchItems = snatchItems;
 }
 }
 String tipMsg = "配置已保存";
 if (!dataChangeFlag) {
 tipMsg = "配置无修改,无需保存!";
 }
 JOptionPane.showMessageDialog(null,tipMsg,"提示",JOptionPane.INFORMATION_MESSAGE);
 }
 });
 }
 
 
 
 
 private boolean validateForm() {
 String tomcatHome = textField1.getText();
 String listenUrl = textField2.getText();
 String interval = textField3.getText();
 if (null == tomcatHome || "".equals(tomcatHome)) {
 JOptionPane.showMessageDialog(null,"请输入tomcat启动脚本位置!","系统提示",JOptionPane.WARNING_MESSAGE);
 return false;
 }
 if (null == listenUrl || "".equals(listenUrl)) {
 JOptionPane.showMessageDialog(null,"请输入监控地址!","系统提示",JOptionPane.WARNING_MESSAGE);
 return false;
 }
 if (!listenUrl.startsWith("http://")) {
 JOptionPane.showMessageDialog(null,"请输入正确的监控地址!","系统提示",JOptionPane.WARNING_MESSAGE);
 return false;
 }
 if (!interval.matches("\d+")) {
 JOptionPane.showMessageDialog(null,"请输入正确的监控间隔(秒)!","系统提示",JOptionPane.WARNING_MESSAGE);
 return false;
 }
 int intervalTime = Integer.parseInt(interval);
 if (intervalTime < 10) {
 JOptionPane.showMessageDialog(null,"监控间隔不能小于10秒!","系统提示",JOptionPane.WARNING_MESSAGE);
 return false;
 }
 if (!tomcatHome.equals(this.tomcatHome)) {
 this.tomcatHome = tomcatHome;
 PropertiesUtils.setString(PropertiesUtils.KEY_TOMCAT_HOME,tomcatHome);
 }
 if (!listenUrl.equals(this.listenUrl)) {
 this.listenUrl = listenUrl;
 PropertiesUtils.setString(PropertiesUtils.KEY_LISTEN_URL,listenUrl);
 }
 if (!interval.equals(this.interval)) {
 this.interval = interval;
 PropertiesUtils.setString(PropertiesUtils.KEY_LISTEN_INTERVAL,interval);
 }
 return true;
 }
 
 
 
 private void loadConf() {
 String tomcatHome = PropertiesUtils.getString(PropertiesUtils.KEY_TOMCAT_HOME);
 String listenUrl = PropertiesUtils.getString(PropertiesUtils.KEY_LISTEN_URL);
 String listenInterval = PropertiesUtils.getString(PropertiesUtils.KEY_LISTEN_INTERVAL);
 String snatchNode = PropertiesUtils.getString(PropertiesUtils.KEY_SNATCH_NODE);
 String snatchUrl = PropertiesUtils.getString(PropertiesUtils.KEY_SNATCH_URL);
 if (null != tomcatHome && !"".equals(tomcatHome)) {
 textField1.setText(tomcatHome);
 this.tomcatHome = tomcatHome;
 }
 if (null != listenUrl && !"".equals(listenUrl)) {
 textField2.setText(listenUrl);
 this.listenUrl = listenUrl;
 }
 if (null != listenInterval && !"".equals(listenInterval)) {
 textField3.setText(listenInterval);
 this.interval = listenInterval;
 }
 if (null != snatchNode && !"".equals(snatchNode)) {
 this.snatchNode = snatchNode;
 snatchNodeFd.setText(snatchNode);
 }
 if (null != snatchUrl && !"".equals(snatchUrl)) {
 snatchUrlField.setText(snatchUrl);
 this.snatchUrl = snatchUrl;
 }
 }
 
 
 
 
 private List<SnatchConfig> getSnatchConfigs() {
 List<SnatchConfig> snatchConfigs = new ArrayList<SnatchConfig>();
 String snatchItems = PropertiesUtils.getString(PropertiesUtils.KEY_SNATCH_ITEMS);
 this.snatchItems = snatchItems;
 if (null != snatchItems && !"".equals(snatchItems)) {
 String[] items = snatchItems.split(",");
 int index = 1;
 for (String item : items) {
 String[] itemConfig = item.split("#");
 boolean startFlag = "0".equals(itemConfig[2]) ? false : true;
 snatchConfigs.add(new SnatchConfig(index,itemConfig[0],itemConfig[1],startFlag));
 index++;
 }
 }
 return snatchConfigs;
 }
 
 
 
 private void startTomcat() {
 Runtime runtime = Runtime.getRuntime();
 try {
 runtime.exec(tomcatHome);
 insertLog("120秒后检测是否启动成功...");
 
 Thread.sleep(120000);
 insertLog("检测是否启动成功...");
 
 String content = HttpClientUtils.get(listenUrl,null,"utf-8");
 if (null != content && !"".equals(content)) {
 
 insertLog("Tomcat启动成功!正在开启抓取项...");
 StartSnatchItemManager.startSnatchItems(this,snatchUrl,snatchNode,snatchItems);
 }
 else {
 insertLog("启动tomcat失败!");
 }
 } catch (IOException e) {
 insertLog("启动tomcat失败!error=" + e.getMessage());
 } catch (InterruptedException e) {
 insertLog("启动tomcat失败!error=" + e.getMessage());
 }
 }
 
 
 
 
 public void insertLog(String msg) {
 StyledDocument sd = logPanel.getStyledDocument();
 try {
 String current = df.format(new Date());
 msg = current + "," + msg +"n";
 int docLength = sd.getLength();
 if (docLength > 100000) {
 sd.remove(0,docLength);
 sd.insertString(sd.getLength(),"日志过多,之前的日志已被清除!", logPanel.getCharacterAttributes());
 }
 sd.insertString(sd.getLength(),msg,logPanel.getCharacterAttributes());
 } catch (BadLocationException e) {
 e.printStackTrace();
 }
 }
 
 
 
 class ListenThread extends Thread {
 private int count = 0;
 public void run() {
 while (listenFlag) {
 try {
 String content = HttpClientUtils.get(listenUrl,null,"utf-8");
 String f = "成功";
 if (null == content) {
 f = "失败";
 count++;
 }
 String msg = "请求URL:" + listenUrl + ",结果:"+ f;
 insertLog(msg);
 
 if (count >= 3) {
 insertLog("tomcat服务监控连续3次异常,重新启动...");
 startTomcat();
 count = 0;
 }
 } catch (Exception e) {
 String msg = "监控异常!error=" + e.getMessage();
 insertLog(msg);
 insertLog("正在重新启动Tomcat...");
 startTomcat();
 } finally {
 try {
 Thread.sleep(1000*Long.parseLong(interval));
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 }
 }
 }
 }
 public static void main(String[] args) {
 JFrame frame = new JFrame("Tomcat监控小工具");
 final TomcatMonitor tm = new TomcatMonitor();
 frame.setContentPane(tm.tp);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setResizable(false);
 frame.pack();
 frame.setSize(900,750);
 frame.setLocationRelativeTo(tm.tp);
 frame.setVisible(true);
 frame.addWindowListener(new WindowAdapter() {
 @Override
 public void windowClosing(WindowEvent e) {
 super.windowClosing(e);
 tm.listenFlag = false;
 }
 });
 }
 }
 
 |