Last active
April 4, 2024 14:37
-
-
Save thomaslagies/b069748025a3bc39aa42fa6fa9ab5737 to your computer and use it in GitHub Desktop.
Send jobs randomly to P5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| usage() { | |
| printf "Usage: $0 [options]\n | |
| -q\tQueue you want to send documents to. Default is a random queue listed in a printers.yml file | |
| -s\tServer hostname or IP address. Default is '127.0.0.1' | |
| -p\tPort. Default is '631' | |
| -n\tNumber of jobs. Default is '1' | |
| -r\tRest n seconds between jobs. Default is 'false' | |
| -f\tFile to print. Default is $0 | |
| -d\tDirectory with files to print randomly from | |
| -c\tNumber of copies. Default is '1'. If set to 'random' copies will be set randomly between 1 and 10\n" | |
| exit 0 | |
| } | |
| # show usage | |
| if [ $# -eq 0 ]; then | |
| usage | |
| fi | |
| # Defaults for variables if not set | |
| SERVER="127.0.0.1" | |
| PORT="631" | |
| MAX="1" | |
| COPIES=1 | |
| function get_file_size { | |
| if [[ "$(uname)" == "Darwin" ]]; then | |
| # Use gstat on macOS | |
| gstat -c%s "$1" | |
| else | |
| # Use stat on other systems | |
| stat -c%s "$1" | |
| fi | |
| } | |
| while getopts ":q:s:p:n:r:f:d:c:h" opt; do | |
| case $opt in | |
| q) QUEUE="$OPTARG" | |
| echo "Set QUEUE to $QUEUE" | |
| ;; | |
| s) SERVER="$OPTARG" | |
| echo "Set SERVER to $SERVER" | |
| ;; | |
| p) PORT="$OPTARG" | |
| echo "Set PORT to $PORT" | |
| ;; | |
| n) MAX="$OPTARG" | |
| echo "Set MAX to $MAX" | |
| ;; | |
| r) REST="$OPTARG" | |
| echo "Set REST to $REST" | |
| if [ "$REST" = "random" ]; then | |
| RANDOM_REST="true" | |
| echo "Wait randomly between jobs for 1-10s" | |
| fi | |
| ;; | |
| f) FILE="$OPTARG" | |
| echo "Set FILE to $FILE" | |
| ;; | |
| d) DIR="$OPTARG" | |
| echo "Set DIR to $DIR" | |
| FILES=($DIR/*) | |
| ;; | |
| c) COPIES="$OPTARG" | |
| echo "Set COPIES to $COPIES" | |
| if [ "$COPIES" = "random" ]; then | |
| RANDOM_COPIES="true" | |
| echo "Set COPIES randomly" | |
| fi | |
| ;; | |
| h) usage | |
| ;; | |
| \?) echo "Invalid option -$OPTARG" >&2 | |
| ;; | |
| esac | |
| done | |
| CURRENT=1 | |
| if [ -z "$QUEUE" ]; then | |
| echo "No queue set, using random printer" | |
| RANDOM_QUEUE="true" | |
| fi | |
| LAST=$(date +%s) | |
| START=$(date +%s) | |
| echo ">>>> Starting to send files at $(date +%H:%M:%S) on $(date +%d-%m-%Y)" | |
| echo "----------------------------------------" | |
| echo "" | |
| # Arrays to hold files of different sizes | |
| tiny_files=() | |
| small_files=() | |
| medium_files=() | |
| large_files=() | |
| xxl_files=() | |
| if [ -n "$FILES" ]; then | |
| # Populate the arrays | |
| for file in "${FILES[@]}"; do | |
| size=$(get_file_size "$file") | |
| if [ $size -lt 1048576 ]; then | |
| tiny_files+=("$file") | |
| elif [ $size -lt 5242880 ]; then | |
| small_files+=("$file") | |
| elif [ $size -lt 20971520 ]; then | |
| medium_files+=("$file") | |
| elif [ $size -lt 104857600 ]; then | |
| large_files+=("$file") | |
| else | |
| xxl_files+=("$file") | |
| fi | |
| done | |
| fi | |
| echo "Tiny file: ${tiny_files[@]}" | |
| echo "Small file: ${small_files[@]}" | |
| echo "Medium file: ${medium_files[@]}" | |
| echo "Large file: ${large_files[@]}" | |
| echo "XXL file: ${xxl_files[@]}" | |
| while [ $CURRENT -le $MAX ]; do | |
| # If queue is not set, send to a random queue listed in a printers.yml file | |
| if [ -n "$RANDOM_QUEUE" ]; then | |
| # Check if yq is installed | |
| if ! command -v yq &> /dev/null; then | |
| echo "yq could not be found. Please install it to continue." | |
| exit 1 | |
| fi | |
| # Get the total number of printers from the YAML file | |
| total_printers=$(yq e '.printers | length' printers.yml) | |
| # Generate a random index | |
| index=$(( RANDOM % total_printers )) | |
| # Use yq to get the printer at the random index | |
| QUEUE=$(yq e ".printers[$index]" printers.yml) | |
| fi | |
| TARGET="ipp://$SERVER:$PORT/printers/$QUEUE" | |
| if [ -n "$FILES" ]; then | |
| # Randomly select a file, with specified probabilities | |
| rand=$(($RANDOM % 1000)) | |
| ADDITION=0 | |
| if [ ${#xxl_files[@]} -eq 0 ]; then | |
| ADDITION=10 | |
| fi | |
| echo $rand | |
| if [ $rand -lt $((590 + ADDITION)) ] && [ ${#tiny_files[@]} -gt 0 ]; then | |
| # 59% | |
| FILE="${tiny_files[$(( RANDOM % ${#tiny_files[@]} ))]}" | |
| elif [ $rand -lt $((790 + ADDITION)) ] && [ ${#small_files[@]} -gt 0 ]; then | |
| # 20% | |
| FILE="${small_files[$(( RANDOM % ${#small_files[@]} ))]}" | |
| elif [ $rand -lt $((890 + ADDITION)) ] && [ ${#medium_files[@]} -gt 0 ]; then | |
| # 10% | |
| FILE="${medium_files[$(( RANDOM % ${#medium_files[@]} ))]}" | |
| elif [ $rand -lt $((990 + ADDITION)) ] && [ ${#large_files[@]} -gt 0 ]; then | |
| # 10% | |
| FILE="${large_files[$(( RANDOM % ${#large_files[@]} ))]}" | |
| elif [ ${#xxl_files[@]} -gt 0 ]; then | |
| # 1% | |
| FILE="${xxl_files[$(( RANDOM % ${#xxl_files[@]} ))]}" | |
| else | |
| echo "No files found in the specified categories." | |
| exit 1 | |
| fi | |
| fi | |
| if [ -z "$FILE" ]; then | |
| # no file set use script file | |
| echo "No file set, using script file" | |
| FILE="${0}" | |
| fi | |
| if [ ! -f "$FILE" ]; then | |
| echo "File '$FILE' does not exist, exiting." | |
| exit 1 | |
| fi | |
| if [ -n "$RANDOM_COPIES" ]; then | |
| COPIES=$(( ( RANDOM % 10 ) + 1 )) | |
| echo "" | |
| echo "Set COPIES randomly to $COPIES" | |
| fi | |
| # creat a uuid | |
| UUID=$(uuidgen) | |
| cat <<EOF >print$QUEUE.$UUID.ipptool | |
| # This is a comment | |
| { | |
| # The name of the test | |
| NAME "Print $FILE" | |
| # The request to send | |
| OPERATION Print-Job | |
| GROUP operation-attributes-tag | |
| ATTR charset attributes-charset utf-8 | |
| ATTR language attributes-natural-language en | |
| ATTR uri printer-uri \$uri | |
| ATTR name requesting-user-name seal-admin | |
| ATTR name job-name "${FILE}" | |
| ATTR text seal-attributes-v2 "SAP_OMS_S_SAPSYSTEMID=sap1","SAP_OMS_S_SPOOLID=123","SAP_OMS_S_RMGID=xyz" | |
| ATTR mimeMediaType document-format application/octet-stream | |
| # ATTR mimeMediaType document-format application/vnd.plossys.native | |
| GROUP job-attributes-tag | |
| ATTR integer copies ${COPIES} | |
| FILE "${FILE}" | |
| # The response to expect | |
| STATUS successful-ok | |
| EXPECT job-id OF-TYPE integer WITH-VALUE >0 | |
| EXPECT job-uri OF-TYPE uri | |
| } | |
| EOF | |
| LAST=$(date +%s) | |
| FILESIZE=$(get_file_size "$FILE") | |
| echo "#$CURRENT - Sending file '$FILE' to printer '$QUEUE' at $(date +%H:%M:%S) on $(date +%d-%m-%Y). File size in Byte: $FILESIZE." | |
| ipptool -tvq -f "$FILE" $TARGET print$QUEUE.$UUID.ipptool; | |
| echo "#$CURRENT - Took $(( $(date +%s) - $LAST ))s to send file '$FILE' to printer '$QUEUE' with $COPIES copies." | |
| CURRENT=$((CURRENT+1)) | |
| rm print$QUEUE.$UUID.ipptool | |
| if [ -n "$RANDOM_REST" ]; then | |
| REST=$(( ( RANDOM % 10 ) + 1 )) | |
| fi | |
| if [ -n "$REST" ]; then | |
| echo "" | |
| echo "#$CURRENT - Sleeping for $REST seconds" | |
| sleep $REST | |
| fi | |
| done | |
| echo "" | |
| echo "----------------------------------------" | |
| echo ">>>> Took $(( $(date +%s) - $START ))s to send $MAX files." | |
| echo ">>>> Finished sending files at $(date +%H:%M:%S) on $(date +%d-%m-%Y)" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment