1 /*------------------------------------------------------------------------------
2 Name: SystemInfoPublisher.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Code for a client to publish system infos to xmlBlaster
6 Version: $Id: SystemInfoPublisher.java 14862 2006-03-07 19:20:19Z goetzger $
7 ------------------------------------------------------------------------------*/
8 package http.dhtml.systemInfo;
9
10 import java.util.logging.Logger;
11 import org.xmlBlaster.util.Global;
12
13 import org.xmlBlaster.util.XmlBlasterException;
14 import org.xmlBlaster.client.I_XmlBlasterAccess;
15 import org.xmlBlaster.client.key.PublishKey;
16 import org.xmlBlaster.client.qos.PublishQos;
17 import org.xmlBlaster.util.MsgUnit;
18
19 import java.util.Random;
20
21
22 /**
23 * Publish system infos to xmlBlaster.
24 * <br />
25 * Use this as a command line tool to publish cpu load and memory load.
26 * <br />
27 * Invoke examples:<br />
28 * <pre>
29 * jaco html.systemInfo.SystemInfoPublisher
30 * </pre>
31 */
32 public class SystemInfoPublisher
33 {
34 private static final String ME = "SystemInfoPublisher";
35 private final Global glob;
36 private static Logger log = Logger.getLogger(SystemInfoPublisher.class.getName());
37 private I_XmlBlasterAccess con;
38 private Random random = new Random();
39
40 /**
41 * Constructs the SystemInfoPublisher object.
42 * <p />
43 * @param args Command line arguments
44 */
45 public SystemInfoPublisher(Global glob) {
46 this.glob = glob;
47
48 /*
49 String osName = System.getProperty("os.name"); // "Linux" "Windows NT" ...
50 if (!osName.startsWith("Linux")) {
51 log.severe("This system load publisher runs only on Linux, sorry about that\n" +
52 "Note that you can use own Perl or C++ or Java publishers to do this task");
53 System.exit(1);
54 }
55 */
56 setUp(); // login
57
58 while(true) {
59 try {
60 int val = random.nextInt(6000); // between 0 and 6 sec
61 if (val < 2000) val = 2000;
62 try { Thread.sleep(val); } catch( InterruptedException i) {}
63 int cpu = getCpuload();
64 publish("cpuinfo", cpu);
65
66 val = random.nextInt(6000); // between 0 and 6 sec
67 if (val < 2000) val = 2000;
68 try { Thread.sleep(val); } catch( InterruptedException i) {}
69 int mem = getMeminfo();
70 publish("meminfo", mem);
71 }
72 catch (XmlBlasterException e) {
73 log.severe(e.getMessage());
74 }
75 }
76
77 //tearDown(); // logout
78 }
79
80 /**
81 * The Linux way ...
82 * <p />
83 * For now it is a random hack
84 */
85 private int getCpuload() throws XmlBlasterException {
86 // String text = FileUtil.readAsciiFile("/proc/cpuinfo");
87 // log.info("cpuinfo=\n" + text);
88 return random.nextInt(100); // hack!
89 }
90
91 /**
92 * The Linux way ...
93 * <p />
94 * For now it is a random hack
95 */
96 private int getMeminfo() throws XmlBlasterException {
97 // String text = FileUtil.readAsciiFile("/proc/meminfo");
98 // log.info("meminfo=\n" + text);
99 int val = random.nextInt(100); // hack!
100 if (val < 11) val = 11;
101 if (val > 96) val = 96;
102 return val;
103 }
104
105 /**
106 * Connect to xmlBlaster and login
107 */
108 private void setUp() {
109 try {
110 con = glob.getXmlBlasterAccess();
111 con.connect(null, null); // Login to xmlBlaster
112 }
113 catch (Exception e) {
114 log.severe(e.toString());
115 e.printStackTrace();
116 }
117 }
118
119 /**
120 * Logout from xmlBlaster
121 */
122 private void tearDown() {
123 con.disconnect(null);
124 }
125
126
127 /**
128 * Construct a message and publish it.
129 */
130 private void publish(String oid, int value) {
131 if (con == null)
132 return;
133
134 String content = "" + value;
135 /*
136 String xmlKey = "<key oid='" + oid + "' contentMime='text/plain' contentMimeExtended='systemInfo'>\n" +
137 " <systemInfo />" +
138 "</key>";
139 */
140 PublishKey key = new PublishKey(glob, oid, "text/plain", "systemInfo");
141 key.setClientTags("<systemInfo />");
142 PublishQos qos = new PublishQos(glob);
143 MsgUnit msgUnit = new MsgUnit(key, content.getBytes(), qos);
144
145 try {
146 con.publish(msgUnit);
147 } catch(XmlBlasterException e) {
148 log.warning("XmlBlasterException: " + e.getMessage());
149 }
150
151 log.info("Published message " + oid + " with value " + content);
152 }
153
154 /**
155 * java html.systemInfo.SystemInfoPublisher
156 */
157 public static void main(String args[]) {
158 new SystemInfoPublisher(new Global(args));
159 }
160 }
syntax highlighted by Code2HTML, v. 0.9.1