diff --git a/scripts/build_pkg/README.stap-buildpkg b/scripts/build_pkg/README.stap-buildpkg new file mode 100644 index 0000000..d87a3de --- /dev/null +++ b/scripts/build_pkg/README.stap-buildpkg @@ -0,0 +1,45 @@ +systemtap distribution framework + +This is free software. +See the COPYING file for redistribution/modification terms. + +Introduction: + +The SystemTap distribution framework aims to: + +1. packages a set of systemtap scripts in a format that circumvents as much +as possible, external dependencies like kernel-debuginfo. +2. generate a package that will self-extract, run the script, and post +process the output with minimal intervention from users. + +Usage: + +stap-buildpkg scriptfile [options] [stap-options] +Options: + -n pkg_name : specify a package name + -v pkg_version : specify a package version + -c pkg_config : provide config parameters through a file + -p post_process script : post processing script + -h help_config : help text for config parameters + All additonal options will be passed to the stap command + +pkg_name and pkg_version will be used to set the name and version of the resulting executable. An optional config file that contains script specific config options and corresponding help entries can also be specified using the -c and -h options respectively. A post processing script can be provided using the -p option.The resulting package is a self-extracting binary. When invoked it would untar itself, execute the script(by invoking staprun) and post process. +The usage for the the resulting package: + + [options] [parameters] + +Options: + * --run -r Runs the scripts + for n minutes where n can be passed as a parameter. The default + value is 10 minutes. Post processing is performed after the + script completes. + * --start -s Invokes the script as a background process. + * --stop -x Stops the script and performs post processing. + * --all Same as --run + * --help Displays this usage text. + +Parameters: + + * time=[x] x is in minutes. Runs the script for x minutes. valid for + --run(-r) o --start(-s) or --all(-a) options only + diff --git a/scripts/build_pkg/stap-buildpkg b/scripts/build_pkg/stap-buildpkg new file mode 100755 index 0000000..dd04d47 --- /dev/null +++ b/scripts/build_pkg/stap-buildpkg @@ -0,0 +1,200 @@ +##!/bin/bash +# +# Script to auto-generate rpms from stap scripts +# + + +#Checking if script is run by root +if [ $EUID -ne 0 ] +then + echo "ERROR :You need to be root to run this !" + exit +fi + +prog=stap-buildpkg + +echo_usage () { + echo $"Usage: $prog scriptfile [options] [stap-options]" + echo $"Options:" + echo $" -n pkg_name : specify a package name" + echo $" -v pkg_version : specify a package version" + echo $" -c pkg_config : provide config parameters through a file" + echo $" -p post_process script : post processing script" + echo $" -h help_config : help text for config parameters" + echo $" All additonal options will be passed to the stap command" +} + +parse_args() { +while [ -n "$1" ]; do + case "$1" in + -n) PKG_NAME="$2" + shift 2 + echo $PKG_NAME + ;; + -v) PKG_VERSION=$2 + shift 2 + echo $PKG_VERSION + ;; + -c) PKG_INPUT_CONFIG=$2 + echo $PKG_INPUT_CONFIG + shift 2 + ;; + -p) PKG_POST_SCRIPT=$2 + echo $PKG_POST_SCRIPT + shift 2 + ;; + -H) PKG_HELP_CONFIG=$2 + echo $PKG_HELP_CONFIG + shift 2 + ;; + *) STAP_OPTIONS=$@ + echo $STAP_OPTIONS + shift $# +esac +done +} + +validate_args() { + if test -z $PKG_NAME; then + PKG_NAME=`basename $script .stp` + fi + if test -z $PKG_VERSION; then + PKG_VERSION=1.0 + fi + if test -z $PKG_RELEASE; then + PKG_RELEASE="0" + fi + PKG_ARCH=`uname -i` +} + +module_gen() { +stap -p4 -k -m $PKG_NAME.ko $script $STAP_OPTIONS +if [ -f $PKG_NAME.ko ] +then + echo " Module $PKG_NAME.ko generated successfully "; +else + echo " Module $PKG_NAME.ko generation failed. Exiting now... " + rm -rf $TEMPDIR + exit; +fi +} + +create_wrappers() { +echo "Creating RPM wrapper from template.."; +echo "s/TEMPLATE_PKG_NAME/$PKG_NAME/g" > $TEMPDIR/sd-installer-patterns +echo "s/TEMPLATE_PKG_VERSION/$PKG_VERSION/g" >> $TEMPDIR/sd-installer-patterns +echo "s/TEMPLATE_PKG_RELEASE/$PKG_RELEASE/" >> $TEMPDIR/sd-installer-patterns +echo "s/TEMPLATE_ARCH/$PKG_ARCH/g" >> $TEMPDIR/sd-installer-patterns + +sed -f $TEMPDIR/sd-installer-patterns template.buildpkg > $TEMPDIR/wrapper +csplit -s $TEMPDIR/wrapper '/extractor template/' +mv xx00 ${PKG_NAME}_install +mv xx01 ${PKG_NAME}_binextractor + +chmod a+x ${PKG_NAME}_install +rm $TEMPDIR/sd-installer-patterns +rm $TEMPDIR/wrapper +} + +copy_files_to_temp() { + +runtime=systemtap-runtime +if test -z $(rpm -qa $runtime); then + runtime=systemtap +fi + +echo "using systemtap runtime executables from $runtime rpm" + +cp `rpm -ql $runtime | grep staprun | grep -v staprun.8.gz` $TEMPDIR/. +cp `rpm -ql $runtime | grep stapio` $TEMPDIR/. + +echo "Copying Systemtap module.." +mv $PKG_NAME.ko $TEMPDIR/ +echo "Copying config file.." +echo "time=2m" > $TEMPDIR/$PKG_NAME.config +if [ -f $PKG_INPUT_CONFIG ] && [ "$PKG_INPUT_CONFIG" != "" ] +then + cat $PKG_INPUT_CONFIG >> $TEMPDIR/$PKG_NAME.config +fi +# Copying help file, or adding an empty file if none is provided. +if [ -f $PKG_HELP_CONFIG ] && [ "$PKG_HELP_CONFIG" != "" ] +then + cp $PKG_HELP_CONFIG $TEMPDIR/$PKG_NAME.help +else + echo " " > $TEMPDIR/$PKG_NAME.help +fi +echo "Copying the post processing script" +# copying the post processing script, or adding an empty file if none is provided. +if [ -f $PKG_POST_SCRIPT ] && [ "$PKG_POST_SCRIPT" != "" ] +then + cp $PKG_POST_SCRIPT $TEMPDIR/$PKG_NAME.post +else + echo " " > $TEMPDIR/$PKG_NAME.post +fi +chmod +x $TEMPDIR/$PKG_NAME.post +mv ${PKG_NAME}_install $TEMPDIR/ +} + +#------------------------------------------------------------------ +# Mainline script +#------------------------------------------------------------------ +script=$1 +echo $script +shift 1 + +if test -z $script; then + echo "Please provide a script name" + echo_usage + exit +fi + +if [ ! -f "$script" ]; then + echo "Script file does not exist" + echo "Please provide a valid script name" + echo_usage + exit +fi + + +#OPTS=`getopt -s bash -u -o 'n:v:R:c:p:h:' -- $@` + +#parse arguments +parse_args $@ +validate_args + +# generate a tmp directory to store all components:remember to clean it up ! +TEMPDIR=/tmp/$PKG_NAME-$PKG_VERSION; +mkdir $TEMPDIR + +# Generate Stap module +echo "Generating Stap module.."; +module_gen + +#generate installer(wrapper) scripts +create_wrappers + +#copy systemtap-runtime files +copy_files_to_temp + +MY_PWD=`pwd` +cd $TEMPDIR ; tar -cjf $PKG_NAME-$PKG_VERSION.tar.bz2 * +cd $MY_PWD; + +echo "Building the self-extracting binary.." +mkdir $TEMPDIR/$PKG_NAME +mv $TEMPDIR/${PKG_NAME}_install $TEMPDIR/$PKG_NAME/ +tar -xjf $TEMPDIR/$PKG_NAME-$PKG_VERSION.tar.bz2 -C $TEMPDIR/$PKG_NAME/ +mv ${PKG_NAME}_binextractor $TEMPDIR/${PKG_NAME}_binextractor 2>/dev/null +#mv /tmp/$PKG_NAME-$PKG_VERSION.tar.bz2 $TEMPDIR/ + +MY_PWD=`pwd` +cd $TEMPDIR; +tar -czf - $PKG_NAME/ >> ${PKG_NAME}_binextractor +cd $MY_PWD; +mv $TEMPDIR/${PKG_NAME}_binextractor ./$PKG_NAME-$PKG_VERSION +echo "The installer is at `pwd`/$PKG_NAME-$PKG_VERSION" +chmod +x $PKG_NAME-$PKG_VERSION + +#Cleanup the mess in temp dir +#rm -rf $TEMPDIR +exit diff --git a/scripts/build_pkg/template.buildpkg b/scripts/build_pkg/template.buildpkg new file mode 100644 index 0000000..8dadb5f --- /dev/null +++ b/scripts/build_pkg/template.buildpkg @@ -0,0 +1,153 @@ +#!/bin/bash +SCRIPT_LOC=/usr/tapsetrpms/TEMPLATE_PKG_NAME-TEMPLATE_PKG_VERSION +RUN_DIR=`pwd` + +function run() +{ +echo "Gathering data... Please wait" +# Log file appears as systemtap_tcpipstat.out +TS=`date "+%Y%m%d-%H:%M:%S"` +export SYSTEMTAP_STAPIO=$RUN_DIR/stapio +$RUN_DIR/staprun $RUN_DIR/TEMPLATE_PKG_NAME.ko > $RUN_DIR/TEMPLATE_PKG_NAME.out 2>&1 & +sleep $time +echo "Completed" +mv $RUN_DIR/TEMPLATE_PKG_NAME.out $RUN_DIR/TEMPLATE_PKG_NAME_${TS}.out +echo "Output file TEMPLATE_PKG_NAME_${TS}.out is in $RUN_DIR directory" +ps -ef | grep TEMPLATE_PKG_NAME | grep stapio | awk '{print $2}' | xargs kill -SIGINT +echo "Postprocessing..." +$RUN_DIR/TEMPLATE_PKG_NAME.post $RUN_DIR/TEMPLATE_PKG_NAME_${TS}.out +rm $RUN_DIR/staprun +rm $RUN_DIR/stapio +} + +function start() +{ + # check to see if tapset is installed +echo "Gathering data..." +export SYSTEMTAP_STAPIO=$RUN_DIR/stapio +$RUN_DIR/staprun $RUN_DIR/TEMPLATE_PKG_NAME.ko > $RUN_DIR/TEMPLATE_PKG_NAME_tmp.out 2>&1 & +echo "Run TEMPLATE_PKG_NAME --stop to stop the script and process output" +} + +function stop() +{ +TS=`date "+%Y%m%d-%H:%M:%S"` +echo "Stopping systemtap script" +ps -ef | grep TEMPLATE_PKG_NAME | grep stapio | awk '{print $2}' | xargs kill -SIGINT +echo "Done" +mv $RUN_DIR/TEMPLATE_PKG_NAME_tmp.out $RUN_DIR/TEMPLATE_PKG_NAME_${TS}.out +echo "Output file TEMPLATE_PKG_NAME_${TS}.out is in $RUN_DIR directory" +echo "Postprocessing..." +$RUN_DIR/TEMPLATE_PKG_NAME.post $RUN_DIR/TEMPLATE_PKG_NAME_${TS}.out +rm $RUN_DIR/staprun +rm $RUN_DIR/stapio +} + +function helptext() +{ + echo " " + echo "USAGE: TEMPLATE_PKG_NAME [options] [parameters]" + echo " " + echo "Options: " + echo " --run -r Runs the scripts for n minutes where n can be passed as a parameter. " + echo " The default value is 10 minutes. Post processing is performed after the script completes." + echo " --start -s Runs the script as a background process" + echo " --stop -x Stops the script and performs post processing" + echo " --all Runs the script and processes the output" + echo " --help Displays this usage text" + echo " " + echo "Parameters: " + echo " " + echo " time=[x] x is in minutes. Runs the script for x minutes. valid for --run(-r)" + echo " --start(-s) or --all(-a) options only" +# echo " ipaddress=[ip] ip is a standard IPv4 address. The output will be filtered for this address" + if [ $? -eq 0 ] + then + cat $RUN_DIR/TEMPLATE_PKG_NAME.help + fi + echo " " +} + +function parseparameters() +{ +for i in $@ +do + tmpa=${1%%=*} + tmpb=${1#$tmpa=} + export $tmpa=$tmpb + shift +done +} + + +#checking if its run by root +if [ $EUID -ne 0 ] +then + echo "You need to be root to run this !" + exit; +fi +option=$1 +shift + + + + FILE=$RUN_DIR/TEMPLATE_PKG_NAME.config + # make sure file exist and readable + if [ ! -f $FILE ]; then + echo "$FILE : does not exist" + elif [ ! -r $FILE ]; then + echo "$FILE: cannot read" + fi + + + + exec 3<&0 + exec 0<$FILE + while read line + do + tmpa=${line%%=*} + tmpb=${line#$tmpa=} + declare $tmpa=$tmpb + done + exec 0<&3 + + for i in $@ + do + tmpa=${1%%=*} + tmpb=${1#$tmpa=} + export $tmpa=$tmpb + shift + done + + +case $option in + -r | --run ) + run + exit + ;; + -s | --start ) + start + exit + ;; + -h | --help ) helptext + exit + ;; + -x | --stop ) stop + exit + ;; + -a | --all ) run + exit + ;; + * ) helptext + exit 1 +esac + +## extractor template +#!/bin/sh -e +sed -e '1,/^exit$/d' "$0" | tar -xzf - +cd TEMPLATE_PKG_NAME + ./TEMPLATE_PKG_NAME_install $@ +cd .. +rm -f TEMPLATE_PKG_NAME/TEMPLATE_PKG_NAME.* +rm -f TEMPLATE_PKG_NAME/TEMPLATE_PKG_NAME_install +exit