Haiku simplest way to archive HAI Omni event logs using HH?

js19707

Active Member
I'm looking for the best way to reliably archive the event logs from my OP2. i had posted a similar question back in December and lupinglade suggested using HH. i recently got a machine set up w/ HH for this purpose, but haven't got the event logging up and running.

my request for help in the other thread didn't a response and i couldn't figure out how to move it to the Haiku/Haikuhelper subforum where it would get more attention from those who can help, i'm posting again here (note to mods: it would be great if you could combine these two threads)

I tried writing the following script but it doesn't appear to properly retrieve the events. the fields are all 'undefined', even though if I go to http://localhost/api/controller.events i get back data that looks correct.

thanks!




Code:
var isCollectingEvents = false;
var lastCollectionTS = 0;
function onRefreshConfig(forced) {
  controller.refreshEvents();
}
function onSyncStart(forced, timestamp) {
  if(!isCollectingEvents) collectEvents();
}
function collectEvents() {
  if(controller.isConnected) { // don't log nonsense if not connected
	isCollectingEvents = true;
	var line = '';
	var count = 0;
	var firstEventSeen = 0;
	var lastEventSeen = 0;
	var collectionTS = controller.date;
	var events = controller.events;
	for (var i in events) { // events are most-recent first
	  var e = events[i];
	  if (e.dateIsValid && e.date <= lastCollectionTS) {
		break;
	  }
	  line+= e.dateDescription + ": " + e.eventDescription + "\n";
	  count++;
	  if (lastEventSeen == 0) {
		lastEventSeen = e.date;
	  }
	  firstEventSeen = e.date;
	}
			  
	lastCollectionTS = collectionTS;
	helper.logToExternalFile('EventLog.txt', count + " events between ["
	  + firstEventSeen + " - " + lastEventSeen + "] collected at "
	  + lastCollectionTS);
	helper.logToExternalFile('EventLog.txt', line);
	helper.log(controller, count + " events between [" + firstEventSeen
	  + " - " + lastEventSeen + "] collected at " + lastCollectionTS);
	setTimeout('collectEvents()', 10*60*1000); // Every 10 minutes
  }
  else {
	isCollectingEvents = false;
  }
}
 
I've traced it down to a bug in HaikuHelper causing this, we've corrected it and the next update will work. I will help you get it adjusted as you need.
 
The script will likely need a few more tweaks because controller.refreshEvents() should be called AND completed before each time you examine the events, so that you analyze the latest ones. I'll push the next update to Apple today, which will include the fix needed.
 
Something along these lines will do what you want once the update is out. Notice i've adjusted the event refresh to happen each time before you start recording them. Queue checkpoints help you sequence it correctly. Now you can set the refresh interval to whatever you like (where its 10 minutes currently), but likely running it every 10 minutes is inefficient and would burden the controller. You can also adjust controller.refreshEvents(0) to a different number than 0 if you only want the last x events to be checked each time, likely there is no sense in refreshing all events every time as that many events can't accumulate so quickly (hopefully).

Code:
var isCollectingEvents = false;
var lastCollectionTS = 0;
 
function onSyncStart(forced, timestamp) {
  if(!isCollectingEvents) collectEvents();
}
 
function collectEvents() {
  if(controller.isConnected) { // don't log nonsense if not connected
	isCollectingEvents = true;
	helper.log(controller, "Refreshing Events...");
	controller.refreshEvents(0);
	controller.addQueueCheckpoint("EventsRefreshed");
  } else {
	isCollectingEvents = false;
  }
}
 
function onQueueCheckpointReach(checkpoint) {
  if(checkpoint == "EventsRefreshed") recordEvents();
}
 
function recordEvents() {
  helper.log(controller, "Recording Events...");
  var line = '';
  var count = 0;
  var firstEventSeen = 0;
  var lastEventSeen = 0;
  var collectionTS = controller.date;
  var events = controller.events;
 
  for (var i in events) { // events are most-recent first
	var e = events[i];
	if (e.dateIsValid && e.date <= lastCollectionTS)  break;
	line+= e.dateDescription + ": " + e.eventDescription + "\n";
	count++;
	if (lastEventSeen == 0) lastEventSeen = e.date;
	firstEventSeen = e.date;
  }
				
  lastCollectionTS = collectionTS;
  helper.logToExternalFile('EventLog.txt', count + " events between [" + firstEventSeen + " - " + lastEventSeen + "] collected at " + lastCollectionTS);
  helper.logToExternalFile('EventLog.txt', line);
  helper.log(controller, count + " events between [" + firstEventSeen + " - " + lastEventSeen + "] collected at " + lastCollectionTS);
  setTimeout('collectEvents()', 10*60*1000); // Every 10 minutes
}
 
thanks lupinglade for the fast response (as always!) and the corrections.
i'll keep an eye out for the updated HH release.
 
This is great I was going to ask this question. Perfect timing! Thanks!

I think I would prefer to email my logs that way I can read them from afar and or have them in the"cloud" rather than in a file on the machine should the drive die or whatever! I think I can out that together.

Thanks

Neil
 
i agree with you abt reliability of hard drives. my plan was to write to a RAIDed NAS share and/or to one of the available cloud-replicated drive share services. email isn't a bad way to go but i already have too much email that i don't read :blush:

i was actually going to split the files based upon date and clean up the logic around retrieving the events to reduce the redundancy and load, as lupinglade suggested. i'll do it once the new version of HH is available.

i can post the updated version here if there's interest in it?
 
I a interested so post away. In terms of email it's small ans I have thousands of email. If I need to look back I can search gmail and find what I am looking for. Gmail is a much a repository for stuff as it is for email.

Thanks

Neil
 
This should do e-mails:

Code:
var isCollectingEvents = false;
var lastCollectionTS = 0;
 
function onSyncStart(forced, timestamp) {
  if(!isCollectingEvents) collectEvents();
}
 
function collectEvents() {
  if(controller.isConnected) { // don't log nonsense if not connected
		isCollectingEvents = true;
		helper.log(controller, "Refreshing Events...");
		controller.refreshEvents(0);
		controller.addQueueCheckpoint("EventsRefreshed");
  } else {
		isCollectingEvents = false;
  }
}
 
function onQueueCheckpointReach(checkpoint) {
  if(checkpoint == "EventsRefreshed") recordEvents();
}
 
function recordEvents() {
  helper.log(controller, "Recording Events...");
  var line = '';
  var count = 0;
  var firstEventSeen = 0;
  var lastEventSeen = 0;
  var collectionTS = controller.date;
  var events = controller.events;
 
  for (var i in events) { // events are most-recent first
		var e = events[i];
		if (e.dateIsValid && e.date <= lastCollectionTS)  break;
		line+= e.dateDescription + ": " + e.eventDescription + "\n";
		count++;
		if (lastEventSeen == 0) lastEventSeen = e.date;
		firstEventSeen = e.date;
  }
								
  lastCollectionTS = collectionTS;
 
  var message = count + " events between [" + firstEventSeen + " - " + lastEventSeen + "] collected at " + lastCollectionTS + "\n";
  message += line;
  helper.sendMail(controller, "[email protected]", "Event report", message);
 
  setTimeout('collectEvents()', 10*60*1000); // Every 10 minutes
}

Of course this will email you every 10 minutes, so don't forget to adjust that appropriately.
 
How often should I do it? I mean I would think once at noon and once at midnight is more than sufficient?

Sorry im a noob and never really good at javascript without hacking it to bits!
 
There is a problem with this script however as it looks like the message to be sent is too large and our server will not allow it through. We'll have to look at either increasing the message size limit or letting HaikuHelper do the actual e-mailing directly using your own SMTP server.
 
Then you would set it to every 12 hours I suppose, or you can adjust the script to do it exactly at a given time. I can write up a sample to do that. The only problem right now is what I mentioned above... we use a very basic e-mailing system right now, I guess we will need to expand it for large messages like these.
 
i hesitate to ask this, but.. if you're going to have to do work to create a more flexible email solution anyway.. it would be really great if you could support sending email via Gmail or Yahoo , i.e. SMTP authentication plus either TLS/STARTTLS or SSL (see http://support.google.com/mail/bin/answer.py?hl=en&answer=13287 and http://help.yahoo.com/l/us/yahoo/smallbusiness/bizmail/pop/pop-33.html). this is probably more reliable and fewer headaches long-term than having to relay SMTP yourself (which i guess is what HH does now?). rather than have to implement yourself, i presume there are open source implementations available under reasonable license terms but honestly i've never looked. at any rate, i figure there's no harm in asking..
 
Back
Top