Tuesday, April 5, 2011

Check process output with Monit workaround

We are monitoring our client's daemons with Monit. This works just fine, but sometimes we would like to run a process and have monit do something depending on its result which is not supported...

So you could just go and run the command from a cron script and do all actions from the cron script. But if you restart a service maybe you run into problems/conflicts with monit, and if you want alerts you have to configure them some other way, so it ends up bing a bit messy.

Another option would be write status to syslog and check for matching lines in monit but this would cause alerts/actions to be performed every time the matching line is logged.

So we're using the following workaround:
  • Run command periodically with cron and save output to a status-file

  • Monitor the status-file checking for a regular expressing


Monit's file content checking is thought for logs - so it tries to read from where it left off last time unless the file shrinks or the inode changes. To make monit reread the file everytime it changes we're using the following wrapper script "monitwrapper.sh":


#!/bin/sh

FILE=$1
shift

#make sure file exists
touch $FILE

#run command and gather output (exit status and stdout)
NEWSTDOUT=`$@`
STATUS=$?
NEWV=$STATUS:$NEWSTDOUT

#get output of last run
OLDV=`cat $FILE`

#if status has changed:
#change inode of the file to make monit reread
if [ "$NEWV" != "$OLDV" ]; then
echo $NEWV > $FILE.tmp
mv $FILE.tmp $FILE
fi


This leaves us with the return status and the stdout written to the status file only if the content is different.

We can run this from cron:

*/5 * * * * root monitwrapper.sh /var/run/teststatus testcmd


And monitor with monit:

check file status with path /var/run/teststatus
if not match "^0" then alert

Saturday, March 5, 2011

Automatic generic typesafe DAO implementation using Guice and Hibernate

Hi,

When you want to use DAO's for your application you will very likely going to use an interface like this:


public interface DAO<T, K extends Serializable> {

T findById(K id);

List<t> list();

void persist(T instance);

void delete(T instance);
}


Using Hibernate for O/R Mapping and Guice as DI framework you can use a sort of template for getting typesafe implementations of DAO's automatically generated for you, and you can ask for them to be injected like this:


public class NeedsDao {

@Inject
public NeedsDao(Dao<MyEntity, Integer> entityDao) {
List<MyEntity> miEntities = entityDao.list();
Entity EntityById = entityDao.findById(3);
}
}


Of course you need to know which type of key you need to use to get your entity, in this case we suppose the key is a plain Integer.

The class that implements the logic for this would be:


public class GenericDAO<T,K> implements DAO<T,K> {

private final Class<T> typeClass;
private final Provider<Session> sessionProvider;

@Inject
public GenericDAO(Provider<Session> sessionProvider, TypeLiteral<T> t) {
this.sessionProvider = sessionProvider;
this.typeClass=(Class<T>)t.getRawType();
}

public List<T> list() {
Session session = sessionProvider.get();
return (List<T>)session.createCriteria(typeClass).list();
}

@SuppressWarnings("unchecked")
@Override
public T findById(K id) {
Session session = sessionProvider.get();
return (T)session.get(typeClass, id);
}

@Override
public void persist(T instance) {
Session session = sessionProvider.get();
session.persist(instance);
}

@Override
public void delete(T instance) {
Session session = sessionProvider.get();
session.delete(instance);
}

}


With this parameterized class we implement all the DAO's we need. We circumvent type erasuring using Guice TypeLiteral injection feature, which allows us for inspecting at runtime which parameter the class has been parameterized with.

Now we only have left to make the wiring in Guice. The easiest way to do this is inspecting Hibernate configuration and generate a binding for each persistent class we have configured. We can write this module:


public class HibernateAutoModule extends AbstractModule {

public HibernateAutoModule(Configuration configuration) {
this.configuration = configuration;

}

@Override
protected void configure() {
configuration.buildMappings();
Iterator<PersistentClass> classMappings = configuration
.getClassMappings();
while (classMappings.hasNext()) {
PersistentClass persistentClass = (PersistentClass) classMappings
.next();
bind(
(TypeLiteral) TypeLiteral.get(Types.newParameterizedType(
DAO.class, persistentClass.getMappedClass(),
persistentClass.getKey().getType()
.getReturnedClass()))).to(
(TypeLiteral) TypeLiteral.get(Types.newParameterizedType(
GenericDAO.class,
persistentClass.getMappedClass(), persistentClass
.getKey().getType().getReturnedClass()))
);
}
}
}


You only need to correctly provide a fresh Configuration object from hibernate and install this module. Note that GenericDAO also depends on you providing a Session object from Hibernate.

Should you want to provide a different implementation for any of the interfaces you can use the Modules.override mechanism from Guice. Just generate a new binding for the interface you want to change the implementation.

I figure that the code won't work for you as is, but I think you can get the idea so you can adapt it for your needs.

Hope this helps!

Tuesday, December 8, 2009

Rendering flash SWF to Video in linux

Recently we came across the need of rendering a flash animation into video directly using linux.
We started doing some searching but nothing stood out as an easy solution. There were, though, a lot of links on how to convert FLV to another video formats (which is a totally different issue).

Then we started to look into the linux open source flash players, and it turns out that Gnash has an experimental feature that allows you to render the animation (including sound) to video and audio files.

Here is explained how to compile Gnash for doing this and how to use some other tools for having an usable video.

This seems to work flawlessly if you don't mind using a hell of disk space since Gnash renders the video as a sequence of uncompressed video frames. Of course we only tried a couple of animations so you may run into some troubles as this feature is supposed to be a bit experimental.

As I said before, this should not be advisable for converting FLV to another video formats (nor to convert animations that use online videos or the like, I guess). There are a lot of easier alternatives to do that (such as ffmpeg).

Hope this helps!

Wednesday, July 29, 2009

Wrapping Python methods maintaining the signature (aka wrapping soapmethod)

We lately came across a problem trying to wrap our server side SOAP methods to do some logging.
Looking through the source code of soaplib the problem was clear - it inspects the func_code of the method for it's arguments and we're losing the method signature when wrapping.

Because when you write a wrapper (for logging for example) you generally write a method using *args and **kwargs, for example:


def wrapMethod(f):
def wrapper(*args, **kwargs):
print "do something before"
#call original method
ret=f(*args, **kwargs)
print "do something after"
return ret


So the catch with this is that the signature that the method callers see is different (basically you can match the method's name, but not much more) - you have lost the parameters of the method.
The soapmethod decorator inspects the func_code of the method to see what arguments it receives. So if you wrap it, you basically break it - something like this:


...
descriptor = func(_soap_descriptor=True,klazz=self.__class__)
File "/usr/lib/python2.5/site-packages/soaplib-0.7.2dev_r27-py2.5.egg/soaplib/service.py", line 39, in explainMethod
in_params = [(_inVariableNames.get(param_names[i],param_names[i]),params[i]) for i in range(0,len(params))]
IndexError: tuple index out of range


Our workaround is to create dynamic code that defines a method with the same signature:


#The original wrapper
def wrapMethod(f):
def wrapper(*args, **kwargs):
print "do something before"
#call original method
ret=f(*args, **kwargs)
print "do something after"
return ret


def literalWrapMethod(f, makeWrapper):
#get the method's parmeters
sargs=", ".join(f.func_code.co_varnames[1:f.func_code.co_argcount])

#dynamic code for the wrapper
code="\
def curryF(f):\n\
def %(origname)s(self, %(args)s):\n\
return f(self, %(args)s)\n\
\n\
return X\
"%{"args":sargs, "origname":f.func_name}

#interpret the method's code
exec(code)
#obtain the wrapper for f
wrp=curryF(makeWrapper(f))
return wrp


#Use the wrapper we wanted to use originally
def logSOAP(f):
return literalWrapMethod(f, wrapMethod)


#Demo on how you'd use this
@soapmethod(soap_types.String,
_returns=soap_types.String)
@logSOAP
def doSomething(inparam):
...


This way we make yet another wrapper with the original signature that calls the generic wrapper. You can obviously avoid one layer writing all the code in the dynamically evaluated code block... but I got tired of writing code in a string ;)

Thursday, June 25, 2009

ValueChangeListeners for JSF elements in loops

When you have editable elements in a looping element, like a datable, or a panelSeries making useful valueChangeListeners get's a bit problematic.


<ice:dataTable value="#{myView.serviceList}"
var="service"
id="ServicesTable"
rows="5">
<ice:column id="column1">
<f:facet name="header">
<ice:outputText value="#{msg.name}"></ice:outputText>
</f:facet>
<ice:outputText effect="#{service.effect}" value="#{service.name}" />
</ice:column>
<ice:column id="c_selector">
<f:facet name="header">
<ice:outputText value="#{msg.selected}"/>
</f:facet>
<ice:selectBooleanCheckbox partialSubmit="true" valueChangeListener="#{myView.changeListener}" />
</ice:column>
</ice:dataTable>


In the valueChangeListener you can consult the source, which is the UIComponent that triggered it - but in such a loop the UIComponent is always the same for every iteration.
This means that you can't even make a different binding for each element in the loop - something like:


<ice:selectBooleanCheckbox binding="#{service.checkboxBinding}" partialSubmit="true" valueChangeListener="#{myView.changeListener}" />


... because there's only one component and faces won't let you do a binding to a changing element.

But there is one option to retrieve the current value of your iterator variable from the listener, through the ExternalContext's requestMap:


public class MyView{
public String changeListener(ValueChangeEvent event){
//event.getSource(); will always be the same UIComponent

FacesContext ctx = FacesContext.getCurrentInstance();
//"service" is the name of the "var" attribute of the datatable
ServiceViewObject service=(ServiceViewObject) ctx.getExternalContext().getRequestMap().get("service");

System.out.prinln("You changed the value for: "+service);
return null;
}
}


The only drawback of this is that you have to have to force the name of the variable in your loop in the JSP file to the name defined in the valueChangeListener.

Wednesday, May 27, 2009

Icefaces PanelConfirmation usage example

Hello there...

recently we needed to add confirmation dialogs on some of the user events of our Icefaces application. Luckily, since version 1.8, Icefaces comes with a new component called PanelConfirmation which does the job pretty well and easy.

As I could not find any straight example I decided to write here a little code snippet.
To define the confirmation panel in the very page you need to write the following:


<ice:panelConfirmation id="confirmDeletion"
acceptLabel="OK"
cancelLabel="Cancel"
message="You are about to delete. Are you sure?"
title="Confirm delete"
draggable="false"
/>

Now you need to bind the dialog with the actions you want to confirm. To do so there is a new property in commandButton and commandLink components. Note the id in the above declaration, it is important to further bind the component. As usual, you will need this id to be unique in the scope.


<ice:commandButton actionListener="#{myActionListener}" value="Remove" panelConfirmation="confirmDeletion" />


In the panelConfirmation property you need to put the id of the dialog.

Regrettably there is still not support for this on the rest of the components that trigger user events, such us menuItems. I don't know if this is coming next or there is any implementation reason that prevents the rest of the components to bind to confirmation dialogs.

I still haven't checked whether you can change the dialog properties binding it to it's Java component when the action fires, as it is delayed until the user accepts in the very panel, nor if the dialog properties can be calculated values as in the rest of the components.

In theory you can define the panel anyway in the application as far as it is defined in the context with the proper id.

Hope this helps!

Saturday, May 9, 2009

GValueArray Properties

Just a quickie: Want some fancy properties for your gstreamer plugin? For example a GValueArray containing structures? Here it goes...

When describing the property, just specify a GArrayValue object and indicate the GstStructure's GType as the element type (you can specify NULL, if you want it to contain any kind of element)


//spec for the GstStructure
GParamSpec *elem_spec=g_param_spec_boxed("mystructure","...",
"...", g_type_from_name("GstStructure"), G_PARAM_READABLE);

g_object_class_install_property (gobject_class, PROP_N,
g_param_spec_value_array ("somename", "...", "...", elem_spec, G_PARAM_READABLE));


So basically that's it for specifiying it - now you simply have to create the structures and stuff them into the array:


GstStructure *pair = gst_structure_new("mystruct", "one", G_TYPE_STRING, name, "two", G_TYPE_VALUE_ARRAY, pnlist, NULL);

GValue gval;
memset(&gval, 0, sizeof(gval));

//init gval with the structure
g_value_init(&gval, pair->type);
g_value_take_boxed(&gval, pair);

//add it to the property
g_value_array_append(self->property, &gval);