/src/com/clavain/workers/MongoWorker.java

https://gitlab.com/goolic/MuninMX · Java · 89 lines · 50 code · 9 blank · 30 comment · 3 complexity · 8082c41ddcd31409cd0f9f9fd5023578 MD5 · raw file

  1. /*
  2. * MuninMX
  3. * Written by Enrico Kern, kern@clavain.com
  4. * www.clavain.com
  5. *
  6. * Licensed to the Apache Software Foundation (ASF) under one
  7. * or more contributor license agreements. See the NOTICE file
  8. * distributed with this work for additional information
  9. * regarding copyright ownership. The ASF licenses this file
  10. * to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance
  12. * with the License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing,
  17. * software distributed under the License is distributed on an
  18. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19. * KIND, either express or implied. See the License for the
  20. * specific language governing permissions and limitations
  21. * under the License.
  22. */
  23. package com.clavain.workers;
  24. import com.mongodb.BasicDBObject;
  25. import com.mongodb.DB;
  26. import com.mongodb.DBCollection;
  27. import com.mongodb.MongoClient;
  28. import com.mongodb.MongoOptions;
  29. import static com.clavain.muninmxcd.m;
  30. import static com.clavain.muninmxcd.mongo_queue;
  31. import static com.clavain.muninmxcd.logMore;
  32. import static com.clavain.muninmxcd.logger;
  33. import java.util.Iterator;
  34. /**
  35. *
  36. * @author enricokern
  37. */
  38. public class MongoWorker implements Runnable {
  39. private DB db;
  40. private DBCollection col;
  41. @Override
  42. public void run() {
  43. String dbName = com.clavain.muninmxcd.p.getProperty("mongo.dbname");
  44. db = m.getDB(dbName);
  45. logger.info("Started MongoWorker");
  46. String plugin = "";
  47. while(true)
  48. {
  49. try
  50. {
  51. BasicDBObject doc = mongo_queue.take();
  52. if(doc != null)
  53. {
  54. plugin = doc.getString("plugin");
  55. // each hostname got its own collection
  56. col = db.getCollection(doc.getString("user_id") + "_" + doc.getString("nodeid"));
  57. doc.removeField("hostname");
  58. doc.removeField("nodeid");
  59. doc.removeField("user_id");
  60. //doc.removeField("plugin");
  61. //db.requestStart();
  62. col.insert(doc);
  63. if(logMore)
  64. {
  65. logger.info("Mongo: Wrote " + plugin + " / " + doc.getString("graph") + " / " + doc.getString("value"));
  66. }
  67. //db.requestDone();
  68. }
  69. else
  70. {
  71. Thread.sleep(50);
  72. }
  73. } catch (Exception ex)
  74. {
  75. logger.fatal("Error in MongoWorker: " + ex.getLocalizedMessage());
  76. ex.printStackTrace();
  77. }
  78. }
  79. }
  80. }