Thursday, August 27, 2015

Active Directory Account Expiration - Java

I'm having to do some work with Active Directory accounts.  Here are some tidbits I hope are helpful.

In my situation, I am working with pwLastSet and accountExpires data which are both date/time based.

Active Directory(AD) dates in some (all?) cases are not based on the normal "C" based time functions which calculate seconds from midnight, Jan. 1st 1970.  Instead, AD uses the number of 100ns intervals from midnight, Jan. 1st 1601. 

Here is some code which calculates the number of 100ns increments between Jan 1st, 1601 and 24 hours in the future from "now".  This uses the new Java 8 date/time classes.  The initial date uses the UTC timezone and the end date is using the local timezone.  The negative 24 (-24) is because of the "minusHours()" method causing me to need to subtract a negative to get the positive(future) offset.  The duration is straight forward and by getting the seconds and multiplying by 10^7 I get the 100ns increments.  I used Math.round() just to go from double to long.  The resulting value is then usable in a context such as setting an AD account expiration.

     ZonedDateTime start = ZonedDateTime.of(1601, 1,1,0,0,0, 0, ZoneId.of("Z")); // UTC 
     LocalDateTime now = LocalDateTime.now().minusHours(-24);  
     ZonedDateTime end = ZonedDateTime.of(now, ZoneId.of("America/New_York")); // Eastern 
     Duration dur = Duration.between(start,end);  
     // result in # of 100ns increments  
     long expirationIncrements = Math.round(dur.abs().getSeconds() * Math.pow(10,7));   
     return expirationIncrements;  

Using SpringLDAP, the call to set accountExpires looks like this.  Note "expiration" here was a Long which is converted to a String for use with the LDAP API.

     final ModificationItem [] mods = new ModificationItem[]  
         {  
           new ModificationItem(DirContext.REPLACE_ATTRIBUTE, 
               new BasicAttribute("accountExpires", 
                    expiration.toString()))  
         };  
   
     ldapTemplate.modifyAttributes(userDN, mods);  

Hoping Jesus blesses your day today!
Scott

Tuesday, August 18, 2015

Jenkins - groovy and programmatic job dependency

I really like Jenkins but it certainly isn't a full fledged general job scheduler.  One aspect of various commercial / general schedulers that I miss is the ability to chain jobs together based upon job status.  A scheduler like CA Autosys handles this pretty well.

I can't really justify the cost of a commercial scheduler and we use Jenkins so what can be done?

We'll I ran into a need and decided to find out.  I need to verify the status of an Apache ServiceMix system.  On occasion (still trying to determine root cause), the connectivity with an ERP drops and isn't reacquired.  When that happens, the only solution that worked so far was to restart the ServiceMix process.

What I did is define a Jenkins job which watches the queue of input data for failures.  When a failure occurs I wanted to restart ServiceMix. It took a bit of searching but I found a way to run a second job on identifying our failure condition.  This second job is responsible for performing the restart and the job is initiated programmatically from the first job.  Below is a chunk of code to get, execute and wait for a job to complete.  Not a lot of error handling here but it hopefully provides enough of an example to successfully copy and reuse. 


   def restartWsiServer()  
   {  
    def job = Hudson.instance.getJob('SM RESTART')  
    def anotherBuild  
    try{  
     def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build))  
     out.println "Waiting for the completion of " + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)  
     anotherBuild = future.get()  
    } catch(CancellationException e)  
    {throw new AbortException("${job.fullDisplayName} aborted.")}  
    out.println HyperlinkNote.encodeTo('/' + anotherBuild.url, anotherBuild.fullDisplayName) + " completed."  
   }  


Hope someone finds this useful.

Thursday, August 6, 2015

Gerbil info for parents

It has been about 8 months since we purchased our 2 gerbils.  This post is just a bit of a parents perspective to go along with the pretty large amount of information on the internet by kids and young adults.  These comments are based on our 2 gerbils alone.

This post isn't really for or against gerbils - it is just things we learned about gerbils or in having a child taking care of gerbils.

Each gerbil can definitely have a different personality.  If compared to cartoon characters from Scooby Doo, one of ours is like Fred while our other gerbil is more like Scooby Doo.  One isn't as easily startled and tends to be more active (runs on the wheel) while the other is a little more skittish but quite quick and agile. 

This is pretty much a brain dump of thoughts.
  • They are pretty easy to care for.  Given food, water, regular cage cleaning and some attention - they seem pretty content.  There are a few other things noted below too though.
  • Pet stores carry food which works well for them.  We use Oxbox Essentials Hamster & Gerbil food as the primary food.  As a treat/supplement we give them All living Things hamster & gerbil daily diet.  Over time, we started looking for more things to treat them with (yes, you start to spoil them) - they seem to like eCotrition Yogies which are a cheese flavor like thing.   
  • They do like some hay to eat and nest with.  
  • They can have distinct likes and dislikes regarding other treats (given in small quantities) like raisins, grapes, strawberries, blue berries, hard cheeses, and even bits of egg.
  • They like to chew is an understatement of epic proportions.  They NEED to chew and you must keep them in constant supply of things to chew. The small wood huts from the pet store will quickly grow new entrances, exits and enlargement of the initial openings.  Plan on adding a new hut every ~6 months or so based on what we saw so far.  Plan on providing new wooden play things on a regular basis. I was building little wooden play things out of craft sticks and it is amazing how fast they will turn something into wood chips.  They are also fond of chewing on clothes - be aware, my sweat pants are a bit aerated now.  A good supply of dried blackberry cane works but they seem to prefer the willow from the pet store so far.  Of course, save all your toilet paper rolls and paper towel rolls for them.  A toilet paper roll is about a 5 minute time to a pile of remnants.  We ended up with some empty Christmas and birthday paper rolls - they acted like they hit the jackpot!  They can also be pretty loud when they chew - depending a bit on what it is too.  This can be loud enough to be distracting if you are in the same room.
  • They are FAST and agile.  Only get them out in an area where they can't slip under a door or out any other holes.  If you have "stuff" piled up - they will use it to hide in/under.  
  • Can can jump 10-12 inches pretty easily.  This was more noticeable when younger and in "home made" play areas using books, cardboard box pieces, etc.  
  • They like to crawl under you.  BE CAREFUL sitting or trying to stand in an area with them - it would be REAL easy to sit or step on one.  If you move a cage/tank to a play area for them - be careful if moving it while they are out since they tend to RUN under things as you try to set the object on the floor.  We have had a couple of close calls because they ran under a tank as we moved it to the area they were playing in.
  • They are most active late in the evening (after ~7pm) and are usually moving around in the morning.  Our wonderful little Thumper will run on his wheel into the night - I have woken to him running at 3am. 
  • They don't tend to bite/nip (much).  They can nip on occasion; when young it was more of a "hm, is this finger edible" test.  Once older it has been more of a "please don't chase me, I don't want to be handled now" or maybe a "you haven't paid attention to me recently".  A few times it was an accident in that they were trying to sample something like my sock while on my foot and got a hold of more than desired (by both of us). They have never broken skin and at this point I don't tend to worry about being bitten.
  • If you have an exercise wheel and the gerbil likes it; the metal ones tend to be quite noisy.  The gerbils can run fast enough that you can hardly see their feet.  Our Thumper has done multiple flips in the thing and flung himself out on a number of times when we were watching. Ours actually seems to like attention when on the wheel - he has us trained to clap after a fast run and he will do sprints and then stop and look your direction as if expecting some clapping.  You may be thinking "yeah, right.." but I think you will find that they do indicate they like attention and find various was of showing that.
  • Ok, lets get this over with;  the gerbils poop a lot.  Of course, the more you feed them..  If you give them "out time" - meaning time outside of their cage/tank - plan on some cleanup.  We have wood floors which is likely better than carpet but still not all that pleasant.  Also note that they tend to kick it up in the air when they take off running - so it will likely find its way into areas outside where they stay/play.  The good news is that it tends to be pretty dry and is easily cleaned up with a tissue or piece of toilet paper.  If some low ceramic bowls are laying around the play area, they do seem to "potty" there more that average but they will go pretty much anywhere though.  If you let the gerbils use a "gerbil ball", you will want to check behind them for any presents they leave behind.
  • Our daughter doesn't like to clean up after then and it shows.  Plan on being involved in the cleanup or at least making sure it is done well enough.
  • There is still interest in them after 8 months but it isn't like having something like a dog.  I think if given the opportunity, our daughter would want the dog and would be "ok with keeping the gerbils too".  
  • They do enjoy crawling on people.  There seems to be enough of a difference in their claws to where one has an easier time climbing up shirts than the other depending on the material.  If you sit cross legged they will stand on your knees and look around. 
  • At around 9 months in age, they still don't tend to sit still for very long when out of their cage - they only sit still to chew on whatever is nearby.  Holding them is possible with one for a short period but the other one is always in motion so it is like holding one of those kids "water snake" toys where it is always in a state of sliding out of your hand (crawling in this case).
  • Our gerbils can be "petted" but only very little.  Mainly if they are eating something they really like.  Otherwise they tend to move away after a few seconds.
  • We have taken the camping in a travel trailer 3 times so far.  Make sure your AC works since they spend their time indoors.  I would not take them outside unless it was an emergency.  They are not too bad to travel with but remember to remove the wheel at night if you are a light sleeper.
  • We have also taken then on a week long vacation down south.  If you don't have other pets, remember that you have them and that hotels may charge you extra for having them or deny your stay if they don't allow pets.  We didn't plan ahead for our trip home and this was a little bit of an issue.

The setup of the 10g plastic fish tank plus 2 story tank topper is a decent amount of living space for our 2 gerbils.  I think that combo was about $200.  You can probably find cheaper, I simply found places that had what I wanted.   Then there are other items - some needed upfront and others just acquired over time.  If you get a cage topper (for an aquarium) then I suggest finding a thin rod (brass works well) at either a craft store or home center.  Bend the end to a small J shape and you can use that to snake though the top of the topper to lift the ramp that rests in the aquarium.  This greatly simplified the removal of the topper without a gerbil getting smacked by the ramp or trying to climb it while removing the topper.
  •  hut(s)
  • chew sticks 
  • food
  • hay
  • treats
  • water bottle (best get one that is a anti-chew style)
  • wheel (7-8 inch version seems the right size)
  • bowl and chinchilla dust (they do seem to like their dust baths) - they do tend to make messes in this as well.  Either sift, carefully extract "stuff" or replace once it starts to get excessively dirty.  You don't have to let them play with it daily but they will use it if you do.  Watching them "flop" in the dust is pretty funny to watch.
  • maybe some ceramic dishes 
    • used under water bottle to catch drips
    • food dish if desired
    • "potty" - just a spot they tend to go in.  This gets used a lot but otherwise the tank itself fills quicker; generally clean the dishes daily.
  • ceramic tiles 
    • I put 4-6 of these in the tank & topper.  They seem to prefer standing on these than the cage wires. I bury some in the bedding in the hopes their digging against them helps  wear their claws/nails down.  The tiles in the topper tend to need daily cleaning also.
  • "pet fences" 
    • We purchased 4 of them.  If you hook 2 together you get a nice ~4-5 ft diameter play area.  The ones we have are only about 9 inches tall so we took 2 more and hooked them on top with twist ties to prevent the gerbils from jumping out easily.
  • Smaller tank for in the car while traveling is nice.  Can let them play in it at other times too.
 Here is a pick of the general setup.  Note; don't leave the gerbils unattended with the towels - it isn't great for them to chew but we do let them "burrow" while they play with us in the play area.


[2015/11/08 - updates]
  1. Both gerbils are still very healthy.
  2. One gerbil has a strong desire to not be caged and has learned over the last 3-4 weeks how to "climb" out of the fenced enclosure. By basically performing a quick hop as high as possible and then quickly grasping the bars with a climbing motion he uses a bit of momentum to reach the top of the fence. It took many attempts to get it right but he now repeats it at will.  It is entertaining to watch - until he is free. Our initial mitigation was to put a ~4-5" strip of cardboard around the top edge of the fence so he couldn't find anything to grip to climb over.  A minor gap one evening though allowed him a short lived freedom.  He enjoyed taunting us as we tried variations of chasing, enticing with food and general corralling.  After about 20 minutes my daughter finally caught him.  It probably could have been sooner but I couldn't stop laughing at the sight of my wife *flat* on the floor with arms outstretched in what I can only describe as a mild form of pinball using her hands to try and keep the gerbil within reach. Trust me, the gerbil was not hurt in this (wish I had video, might have been a winner on "America's Funniest Home Videos") - I think my wife was more traumatized than the gerbil (not real found of gerbils that close up to her face).  A few other escape attempts were close - we had to move the tank/topper to nearer the center of the fenced area since it appears he was able to jump onto the outside of the topper in the back and then climb/jump to the flat cardboard area.  So far, our changes are working but we'll likely have to continue coming up with new ways to keep him in.  It seems a bit like "Escape from Alcatraz".
  3. The same escape artist gerbil is a bit nippy at times recently.  I am starting to equate the timing with having handled foods he likes (almonds being the common one).  He starts with sniffing the fingers and then attempts to gnaw.  He follows fingers when you move and keeps trying - it is a bit annoying but now I try not to handle food before handling him.  Washing hands *really well* before handling him helps a bit but I think he can still tell.  I don't think he is trying to purposely hurt me (or others).  He just doesn't seem to differentiate between the smell of food and the real thing as well as his brother.  His brother doesn't exhibit this behavior. 
  4. We are now putting down builders paper (at Lowes) under the overall play area.  This gets replaced weekly during cage cleaning.  It helps to contain their various "outputs" a bit and reduces the amount of damp mopping needed on the hardwood floor. 
  5. If you sit with them and they hang out behind you a lot - best to keep an eye on what they are doing - I now have a pair of sweat pants which I can't wear in public because of them.. You may think they are only bumping/pushing on you but that is likely not what they are up to. :)  You've been warned..