StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TxUCMCollector.h
1 #ifndef STAR_TxUCMCollector
2 #define STAR_TxUCMCollector
3 /*
4  * UCM Collector
5  *
6  */
7 /*****************************************************************
8  * @file TxUCMCollector.h
9  * @author Roopa Pundaleeka
10  *
11  * @(#)cpp/api:$Id: TxUCMCollector.h,v 1.8 2010/09/17 19:34:54 fine Exp $
12  *
13  * Please see TxUCMCollector.h for more documentation.
14  * "Translated" from the original TxUCMCOllector.java version
15  */
16 
17 #include <map>
18 #include <string>
19 #include "mysql.h"
20 #include <log4cxx/logger.h>
21 
22 #include "StDbFieldI.h"
23 #include "TxEventLog.h"
24 
25 typedef bool boolean;
26 typedef std::string Options;
27 
28 namespace TxLogging {
29 
30 class StRecord;
31 class StUcmTask;
32 class StUcmTasks;
33 class StUcmJobs;
34 class StUcmEvents;
35 class FieldList;
36 class RecordList;
37 
38 class TxUCMCollector : public TxEventLog{
39 
43  public:
44  TxUCMCollector ();
45  virtual ~TxUCMCollector ();
46 
52  boolean initDb ();
58  private:
59  boolean init ();
65  boolean loadDatabase();
66  MYSQL *getConnection();
67  MYSQL *getConnection (const char *dbUrl,const char *dbUsername
68  , const char *dbPassword);
69  MYSQL *getConnection (const std::string &dbUrl,const std::string &dbUsername
70  , const std::string &dbPassword);
71  unsigned int execute(const std::string &sql);
72  unsigned int execute(const char *sql);
80  virtual void closeConnection();
86  public:
87  void startProcess ();
103  private:
104  void setCurrLogFile ();
110  void processLogFile ();
118  public:
119  void processMessage (const std::string &message);
120  void processMessage (const char *message);
126  virtual StUcmTasks *getTaskList ();
127  virtual StUcmTasks *getTaskList (int limit);
128  virtual StUcmTasks *getTaskList (int limit, int offset);
129 
130  virtual StUcmJobs *getJobList();
131  virtual StUcmJobs *getJobList(StRecord *task);
132  virtual StUcmJobs *getJobList(StRecord *task, int limit);
133  virtual StUcmJobs *getJobList(int limit);
134  virtual StUcmJobs *getJobList(int limit, int offset);
135  virtual StUcmJobs *getJobList(StRecord *task, int limit, int offset);
136  virtual int getJobId(const char * reqName, const char *taskBrokerId, int jobBrokerId);
137 
138  virtual StUcmEvents *getEventList();
139  virtual StUcmEvents *getEventList(StRecord *job);
140  virtual StUcmEvents *getEventList(StRecord *job,int limit);
141  virtual StUcmEvents *getEventList(int limit);
142  virtual StUcmEvents *getEventList(int limit, int offset);
143  virtual StUcmEvents *getEventList(StRecord *job,int limit, int offset);
144 
145  int fillTaskList(StUcmTasks &tasks,int limit=0, int offset=0);
146  int fillJobList(StUcmJobs &jobs,int limit=0, int offset=0);
147  int fillJobList(StUcmJobs &jobs,int limit, int offset,const StRecord *task);
148  int fillEventList(StUcmEvents &event,int limit=0, int offset=0,const StRecord *job=0);
149  void fillFields(FieldList &fields);
150  int fillUcmList(const char *type,RecordList &records, int limit=0, int offset=0);
151  int queryTableSize(const char *tableName);
152  int queryTableSize(const char *tableName, const char *where);
153  int queryTableSize(const char *tableName, const StRecord *where);
154  void queryTable(const char *tableName,int limit=0, int offset=0, const char *where=0);
155  void queryTaskTable(int limit=0, int offset=0);
156  void queryJobTable(int limit=0, int offset=0);
157  void queryJobTable(const char *taskID,int limit=0, int offset=0);
158  void queryEventTable(int limit=0, int offset=0);
159  void queryEventTable(const char *jobID,int limit=0, int offset=0);
160 
161  private:
162  StDbFieldI *createField(unsigned int fieldIndx);
163  static StDbFieldI::EDataType MapSqlField(enum_field_types type);
164  void createNewTask ();
165  std::string tableNamePrefix(const char *) const;
166  std::string jobTableName() const;
167  std::string eventTableName() const;
168  void setBrokerJobID(const StRecord *job);
169  void setBrokerTaskID(const StRecord *job);
175  void updateTask ();
182  void addJob ();
188  void updateJob ();
193  void setJobsField (const std::string &fieldName);
194  void setJobsField (const char * fieldName);
201  void addEvent ();
205  void createJobsTable ();
209  void createEventsTable ();
210 
218  void insertRecord (const char * insertStr, const char * tableName);
219  void insertRecord (const std::string&insertStr, const std::string&tableName);
228  void updateRecord (const char * updateStr, const char * tableName, const char * condition);
229  void updateRecord (const std::string&updateStr, const std::string&tableName, const std::string&condition);
236  boolean recordExists (const char * selectStr, const char * tableName);
237  boolean recordExists (const std::string&selectStr, const std::string&tableName);
244  void createTable (const char * table, const char *like=0);
245  void createTable (const std::string &table, const std::string &like=std::string());
251  static void usage (Options options);
256  void printVersion();
257 
261  public:
262  static void main(const char *args[]);
266  private:
267  static const char*fgTs;
268  static const char*fgEvent;
269  static const char*fgBJobID;
270  static const char*fgBTaskID;
271  static const char*fgRequester;
272  static const char*fgContext;
273  static const char*fgLevel;
274  static const char*fgStage;
275  static const char*fgKey;
276  static const char*fgValue;
277 
278  static const char*fgNewTask;
279  static const char*fgUpdateTask;
280  static const char*fgAddJob;
281  static const char*fgUpdateJob;
282  static const char*fgSiteLocation;
283  static const char*fgStateID;
284  static const char*fgGridJobID;
285  static const char*fgAppStart;
286  static const char*fgAppEnd;
287 
288  static const char*fgStatusFile;
289  static const char*fgStatusFileName;
290  static const char*fgStatusFileModTime;
291  static const char*fgStatusFilePos;
292 
293 
294  // Class logger
295  private:
296  log4cxx::LoggerPtr log;
297 
298  // global connection for updating UCMDB
299  MYSQL *connection;
300  MYSQL_RES *fResult;
301  MYSQL_FIELD *fField;
302  MYSQL_ROW fRow;
303 
304  bool fIsConnectionOpen;
305  // db info
306  std::string dbName;
307  std::string dbUrl;
308  std::string dbUsername;
309  std::string dbPassword;
310 
311  // logs dir as set by user to read log files from
312  std::string logsDir;
313 
314  // sleep time as set by user (default = 10 seconds)
315  long sleepTime;
316 
317  // current log file thats being processed
318  const char * currLogFile;
319 
320  // current position in the log file thats being processed
321  long currLogFilePos;
322 
323  // hash map to hold key value pairs from a message
324  std::map<std::string,std::string> msgHashMap;
325  // Current broker Job ID to query
326  int fBrokerJobID;
327  // Current Job Db ID to query
328  int fDbJobID;
329 
330  // Task table column names
331  static const char * fgTaskCols;
332 
333  // Jobs table columns
334  static const char *fgJobsTableCols;
335 
336  // Jobs table column names
337  static const char * fgJobCols;
338 
339 
340  // Events table columns
341  static const char * fgEventsTableCols;
342 
343  // Events table column names
344  static const char * fgEventCols;
345 
346 // TxEventLog interface
347 protected:
348  virtual void writeDown(const std::string& message);
349  virtual void writeDown(const char *message);
350 public:
351 //___________________________________________________________________________________________________
360  virtual void setEnvBrokerTaskID (const std::string& envBrokerTaskID);
361 
370  virtual void setEnvBrokerJobID (const std::string& envBrokerJobID);
371 
379  virtual void setBrokerTaskID (const std::string& brokerTaskID);
380 
388  virtual void setBrokerJobID (int brokerJobID);
395  virtual void setDbJobID (int dbJobID);
396 
403  virtual void setRequesterName (const std::string& requester);
404 
410  virtual void setContext (const std::string& context);
411 
416  virtual void logStart (const std::string& key, const std::string& value);
417 
426  virtual void logJobAttribute (const std::string& key, const std::string& value);
434  virtual void logJobSubmitLocation (const std::string& url);
435  virtual void setJobSubmitLocation (const std::string& url);
436 
444  virtual void logTask (unsigned int size=1);
445 
457  virtual void logTask (const std::string& taskAttributes);
464  virtual void logJobSubmitState (State state);
465  virtual void setJobSubmitState (State state);
466 
474  virtual void logJobSubmitID (const std::string& ID);
475  virtual void setJobSubmitID (const std::string& ID);
476 
487  virtual void logEvent (const std::string& logMsg,
488  Level level = LEVEL_INFO,
489  Stage stage = STATUS,
490  const std::string& msgContext = TxUCMConstants::defaultContext);
491 
503  virtual void logEvent (const std::string& userKey,
504  const std::string& userValue,
505  Level level = LEVEL_INFO,
506  Stage stage = STATUS,
507  const std::string& msgContext = TxUCMConstants::defaultContext);
508 
513  virtual void logEnd (const std::string& key, const std::string& value);
514 
515 };
516 }
517 
518 #endif
virtual void setContext(const std::string &context)
static void main(const char *args[])
void processMessage(const std::string &message)
virtual void logEvent(const std::string &logMsg, Level level=LEVEL_INFO, Stage stage=STATUS, const std::string &msgContext=TxUCMConstants::defaultContext)
virtual void logTask(unsigned int size=1)
virtual void setEnvBrokerJobID(const std::string &envBrokerJobID)
virtual void setEnvBrokerTaskID(const std::string &envBrokerTaskID)
virtual void logEnd(const std::string &key, const std::string &value)
virtual void logJobSubmitState(State state)
virtual void logJobSubmitID(const std::string &ID)
virtual void setRequesterName(const std::string &requester)
virtual void logStart(const std::string &key, const std::string &value)
virtual void setDbJobID(int dbJobID)
virtual void logJobAttribute(const std::string &key, const std::string &value)
virtual void logJobSubmitLocation(const std::string &url)
virtual StUcmTasks * getTaskList()