Tuesday, May 8, 2012

hunting computers in Active Directory

This quick batch script allow you to quickly search active directory for computers and grab attributes using very little typing.  It can also be very powerful depending on how creative you get with it.

@echo off
REM this section is just to setup a switch in the script that allows you to grab all attributes from an object.
REM I'm only giving myself the chance to either use the switch in the first or second variable but feel free to
REM add more.
if "%1"=="/a" goto :a1
if "%2"==/a" goto :a2

REM This means that I will be searching through Active Directory with the object's name in question as my first
REM variable. Any word that I use after that can be an Object's Attribute that I wish to grep.
dsquery * -filter "(name=%1)" -attr %*

goto :end

:a1
REM This will pull all the attributes if your switch (/a) is the first variable.  It's handy if you can't remember an
REM attributes exact syntax
dsquery * -filter "(name=%2)" -attr *
goto :end
:a2
REM This will pull all attributes if your switch (/a) is the second variable.
dsquery * -filter "(name=%1)" -attr *

:end

This script requires the W2k3 Administrator's Tools on WinXP, or RSAT on Vista/7.  Some useful attributes to grep are:
lastLogonTimestamp
operatingSystem
distinguishedName
whenCreated
whenChanged
memberOf

Tuesday, April 3, 2012

Extension Renamer

The other day I wanted to change CSV files TXT files for some reason.  The weird part was that they were in spread out in a directory tree, and that I also wished to preserve the directory structure.
WHAM

for /r c:\SOURCE %i in (*.csv) do mkdir "c:\DESTINATION%~pi"& move "%~fi" "c:\DESTINATION%~pi%~ni.txt"

Because I am wishing to create a mirror of my original directory tree (and not do a in-place upgrade), the easiest way to do it was have a SOURCE tree, and a DESTINATION tree, so replace these variables in the command above.  

What this loop is doing is searching through the tree for any files of a CSV extension.  When it finds one, it will create the path in the DESTINATION tree, and then move the file into the corresponding folder, stripping the CSV extension and appending the TXT extension.

As a side note, it is interesting that as a default in Windows mkdir will automatically create intermediate directories if they do not exist.  This is different from Linux, in which you need to append a -p switch to make it so.  You can turn on command extensions as they are called, by running cmd /e:on or off with  cmd /e:off in Windows when launching your shell.

In this Recursive for loop I am taking advantage of special FOR variables to make my substitutions.  For a full list, check 'for /?'.  I recommend doing so, it is full of useful hacks you can do to make an ordinary loop extremely powerful.
%~pi stands for Path Only
%~fi stands for Fully Qualified Path Name
%~ni stands for Name Only


Luckily I did not care about any other files that were not CSV's in this structure.  If I had, I would have probably re-run the loop, changing out CSV for the target extension, and doing a simple move, instead of stripping the iterator's value to the path minus then extension and tacking on the extension.


Like so
for /r c:\SOURCE %i in (*.doc) do mkdir c:\DESTINATION\%pi"& move "%~fi" "c:\DESTINATION%~pi%~nxi


%~nxi stands for Name with Extension

Tuesday, February 14, 2012

Squid Monitoring with Zabbix

This is a quick post to detail monitoring of a Squid 3.1 Proxy server using Zabbix.  This assumes you already have these programs installed and working, including the Zabbix-agent on the proxy server.  I am writing this here because I got frustrated trying to update the Zabbix wiki.  The reason I had to update is that Squid 3+  outputs 'squidclient' commands differently then version 2.

Anyways, I followed the instructions located here: http://www.zabbix.com/wiki/howto/monitor/web/squid.  My updated config is here:


UserParameter=squid.http_requests,squidclient mgr:info|grep 'Number of HTTP requests received:'|cut -d':' -f2| tr -d ' \t'
UserParameter=squid.clients,squidclient mgr:info|grep 'Number of clients accessing cache:'|cut -d':' -f2| tr -d ' \t'
UserParameter=squid.icp_received,squidclient mgr:info|grep 'Number of ICP messages received:'|cut -d':' -f2| tr -d ' \t'
UserParameter=squid.icp_sent,squidclient mgr:info|grep 'Number of ICP messages sent:'|cut -d':' -f2| tr -d ' \t'
UserParameter=squid.icp_queued,squidclient mgr:info|grep 'Number of queued ICP replies:'|cut -d':' -f2| tr -d ' \t'
UserParameter=squid.htcp_received,squidclient mgr:info|grep 'Number of HTCP messages received:'|cut -d':' -f2| tr -d ' \t'
UserParameter=squid.htcp_sent,squidclient mgr:info|grep 'Number of HTCP messages sent:'|cut -d':' -f2| tr -d ' \t'
UserParameter=squid.req_fail_ratio,squidclient mgr:info|grep 'Request failure ratio:'|cut -d':' -f2| tr -d ' \t'
UserParameter=squid.avg_http_req_per_min,squidclient mgr:info|grep 'Average HTTP requests per minute since start:'|cut -d':' -f2| tr -d ' \t'
UserParameter=squid.avg_icp_msg_per_min,squidclient mgr:info|grep 'Average ICP messages per minute since start:'|cut -d':' -f2| tr -d ' \t'
UserParameter=squid.request_hit_ratio,squidclient mgr:info|grep 'Request Hit Ratios:'|cut -d':' -f3|cut -d',' -f1|tr -d ' %'
UserParameter=squid.byte_hit_ratio,squidclient mgr:info|grep 'Hits as % of bytes sent:'|cut -d':' -f4|cut -d',' -f1|tr -d ' %'
UserParameter=squid.request_mem_hit_ratio,squidclient mgr:info|grep 'Hits as % of all requests:'|cut -d':' -f4|cut -d',' -f1|tr -d ' %'
UserParameter=squid.request_disk_hit_ratio,squidclient mgr:info|grep 'Disk hits as % of hit requests:'|cut -d':' -f4|cut -d',' -f1|tr -d ' %'
UserParameter=squid.servicetime_httpreq,squidclient mgr:info|grep 'HTTP Requests (All):'|cut -d':' -f2|tr -s ' '|awk '{print $1}'
UserParameter=squid.process_mem,squidclient mgr:info|grep 'Process Data Segment Size via sbrk'|cut -d':' -f2|awk '{print $1}'
UserParameter=squid.cpu_usage,squidclient mgr:info|grep 'CPU Usage:'|cut -d':' -f2|tr -d '%'|tr -d ' \t'
UserParameter=squid.cache_size_disk,squidclient mgr:info|grep 'Storage Swap size:'|cut -d':' -f2|awk '{print $1}'
UserParameter=squid.cache_size_mem,squidclient mgr:info|grep 'Storage Mem size:'|cut -d':' -f2|awk '{print $1}'
UserParameter=squid.mean_obj_size,squidclient mgr:info|grep 'Mean Object Size:'|cut -d':' -f2|awk '{print $1}'
UserParameter=squid.filedescr_max,squidclient mgr:info|grep 'Maximum number of file descriptors:'|cut -d':' -f2|awk '{print $1}'
UserParameter=squid.filedescr_avail,squidclient mgr:info|grep 'Available number of file descriptors:'|cut -d':' -f2|awk '{print $1}'