Please Help: How can I modify this shell script.

Discussion in 'Programming/Scripts' started by palak sharma, Apr 3, 2014.

  1. palak sharma

    palak sharma New Member

    I have a script that do:

    1. extract the tags(name,owner,cost.centre) from ec2-describe-instances and write this detail to a file.
    2. Also finds the instances whose any above mentioned tag is missing.

    But it writes detail in a single file.



    #!/bin/bash
    out="Instances"
    ec2-describe-instances | awk -F'\t' -v of="$out" '
    function pr() { # Print accumulated data
    if(id != "") { # Skip if we do not have any unprinted data.
    printf(fmt, id, f[1], f[2], f[3]) > of
    if(f[1] == "") print id > "missing_name"
    if(f[2] == "") print id > "missing_owner"
    if(f[3] == "") print id > "missing_cost.centre"
    }
    # Clear accumulated data.
    id = f[1] = f[2] = f[3] = ""
    }

    BEGIN { # Set the printf() format string for the header and the data lines.
    fmt = "%-12s %-33s %-15s %s\n"
    # Print the header
    printf(fmt, "Instance id", "Name", "Owner", "Cost.centre") > of
    }
    $1 == "TAG" {
    # Save the Instance ID.
    id = $3
    if($4 ~ /[Nn]ame/) fs = 1 # Name found
    else if($4 ~ /[Oo]wner/) fs = 2 # Owner found
    else if($4 ~ /[Cc]ost.[Cc]ent[er][er]/) fs = 3 # Cost center found
    else next # Ignore other TAGs
    f[fs] = $5 # Save data for this field.
    }
    $1 == "RESERVATION" {
    # First line of new entry found; print results from previous entry.
    pr()
    }
    END { # EOF found, print results from last entry.
    pr()
    }'
    if [ -e missing_name ]
    then printf "\nThe following Instance ids are missing names:\n" >> "$out"
    cat missing_name >> "$out"
    rm missing_name
    fi
    if [ -e missing_owner ]
    then printf "\nThe following Instance ids are missing owners:\n" >> "$out"
    cat missing_owner >> "$out"
    rm missing_owner
    fi
    if [ -e missing_cost.centre ]
    then printf "\nThe following Instance ids are missing cost centres:\n" >> "$out"
    cat missing_cost.centre >> "$out"
    rm missing_cost.centre
    fi


    The script is producing the output in a single file:


    Code:
    Instance id Name Owner Cost.centre
    i-c4260db8 Rishi_Win_SAML Rishi Pandey 010-9487
    i-7f4b9300 Sachin_Test Sachin Kumar 010-9487
    i-fb5ca283 CLIQR-DO NOT TOUCH matt 010-9487
    i-44ea8122 sachin_RedHat_VPC
    i-b2f9d8d4 SSO_UBUNTU Gaurav Vinayak 010-9487
    i-07190e28 bacula-server:Harsh harsh 010-9487
    i-9ca559b2 bacula-client:Harsh harsh 010-9487
    i-f637cbd8 Rishi_WindowsMachine Rishi Pandey 010-9487
    i-6879b446 bacula-win-client:Harsh harsh 010-9487
    i-057bb22b Tivoli 1 gaurav vinayak 010-9487
    i-887ab3a6 Tivoli 2 Gaurav Vinayak 010-9487
    i-31b6101f Autoscale_Test_Server Jyoti Bhanot 010-9487
    i-9de654b3 splunk_test_VM Sachin 010-9487
    i-585b2d76 AutoScale_Jyoti Jyoti Bhanot 010-9487
    i-6b0c4645 phywncapp01-prd_AWS Sachin 010-9487
    i-fa1953d4 F5_sachin
    i-e0f1a2ce ecswiki01 matt 010-9487
    i-2da2ff03 Test_sachin_Migration
    i-77c9e859 Sachin_Jenkins_Tomcat
    i-3593b31b CLIQR-DO NOT TOUCH matt 010-9487
    i-7ca28d52 FTP_Test01
    i-e62012c8 SAML_Test Sachin,Rishi 010-9487
    i-e7bab6c9 WA1NETA_SingleDisk_Centos_6_64
    i-b8c1ce96 SingleDisk_Centos_6_64_Migration
    i-58dad076 RDS_Machine (us-east-1d) Jyoti Bhanot 010-9487
    i-73f51252 Win_Ins Rishi Pandey 010-9487
    i-6fca9b3e Chef POC Server Gaurav Vinayak 010-9487
    i-d4411185 Rishi_Win_Okta
    i-bad68beb Test_Script
    i-8dfbc9ac Test_LB_Server_01 Sachin Kumar
    i-ef1524cf Hadoop_test Naveen 010-9487
    i-7446b957 Test_LB_Server_02 Sachin Kumar
    i-551ec976 RDS_Machine (us-east-1c) Jyoti Bhanot 010-9487

    The following Instance ids are missing owners:
    i-44ea8122
    i-fa1953d4
    i-2da2ff03
    i-77c9e859
    i-7ca28d52
    i-e7bab6c9
    i-b8c1ce96
    i-d4411185
    i-bad68beb

    The following Instance ids are missing cost centres:
    i-44ea8122
    i-fa1953d4
    i-2da2ff03
    i-77c9e859
    i-7ca28d52
    i-e7bab6c9
    i-b8c1ce96
    i-d4411185
    i-bad68beb
    i-8dfbc9ac
    i-7446b957


    I want to write the following input in a file that contains the detail of all the instances that have all the tags (name,owner,cost.centre) called
    Code:
    "InstanceDetails_CurrentDate"

    where date is systems current date


    Code:
    Instance id Name Owner Cost.centre
    i-c4260db8 Rishi_Win_SAML Rishi Pandey 010-9487
    i-7f4b9300 Sachin_Test Sachin Kumar 010-9487
    i-fb5ca283 CLIQR-DO NOT TOUCH matt 010-9487
    i-44ea8122 sachin_RedHat_VPC
    i-b2f9d8d4 SSO_UBUNTU Gaurav Vinayak 010-9487
    i-07190e28 bacula-server:Harsh harsh 010-9487
    i-9ca559b2 bacula-client:Harsh harsh 010-9487
    i-f637cbd8 Rishi_WindowsMachine Rishi Pandey 010-9487
    i-6879b446 bacula-win-client:Harsh harsh 010-9487
    i-057bb22b Tivoli 1 gaurav vinayak 010-9487
    i-887ab3a6 Tivoli 2 Gaurav Vinayak 010-9487
    i-31b6101f Autoscale_Test_Server Jyoti Bhanot 010-9487
    i-9de654b3 splunk_test_VM Sachin 010-9487
    i-585b2d76 AutoScale_Jyoti Jyoti Bhanot 010-9487
    i-6b0c4645 phywncapp01-prd_AWS Sachin 010-9487
    i-fa1953d4 F5_sachin
    i-e0f1a2ce ecswiki01 matt 010-9487
    i-2da2ff03 Test_sachin_Migration
    i-77c9e859 Sachin_Jenkins_Tomcat
    i-3593b31b CLIQR-DO NOT TOUCH matt 010-9487
    i-7ca28d52 FTP_Test01
    i-e62012c8 SAML_Test Sachin,Rishi 010-9487
    i-e7bab6c9 WA1NETA_SingleDisk_Centos_6_64
    i-b8c1ce96 SingleDisk_Centos_6_64_Migration
    i-58dad076 RDS_Machine (us-east-1d) Jyoti Bhanot 010-9487
    i-73f51252 Win_Ins Rishi Pandey 010-9487
    i-6fca9b3e Chef POC Server Gaurav Vinayak 010-9487
    i-d4411185 Rishi_Win_Okta
    i-bad68beb Test_Script
    i-8dfbc9ac Test_LB_Server_01 Sachin Kumar
    i-ef1524cf Hadoop_test Naveen 010-9487
    i-7446b957 Test_LB_Server_02 Sachin Kumar
    i-551ec976 RDS_Machine (us-east-1c) Jyoti Bhanot 010-9487




    and the missing tags details in separate file named"MissingTagsInstances_Currentdate"
    with output like this. Even if any tag (name,owner,cost.centre)
    is missing, the script should check and should write missing in there.


    Code:
    Instance id Name Owner Cost.centre

    i-44ea8122 sachin_RedHat_VPC Unknown Unknown
    i-fa1953d4 F5_sachin Unknown Unknown
    i-2da2ff03 Test_sachin_Migration Unknown Unknown
    i-77c9e859 Sachin_Jenkins_Tomcat Unknown Unknown
    i-7ca28d52 FTP_Test01 Unknown Unknown
    i-e7bab6c9 WA1NETA_SingleDisk_Centos_6_64 Unknown Unknown
    i-b8c1ce96 SingleDisk_Centos_6_64_Migration Unknown Unknown
    i-d4411185 Rishi_Win_Okta Unknown Unknown
    i-bad68beb Test_Script Unknown Unknown
    i-7446b957 Test_LB_Server_02 Sachin Kumar Unknown

    SO that i can email these two files to two separate persons.

    the ec2-describe-command output is attached hereby.

    any lead is appreciated
    Thanks
     

    Attached Files:

  2. srijan

    srijan New Member HowtoForge Supporter

    Hi

    From where(source of information) you are the calling the values? If you are searching with the timestampts of Active directory then there is a huge difference between the timestamps of an Active directory with the Linux bash.

    What is your desired format of the output, place a sample type example.

    Br//
    Srijan
     
  3. sara736

    sara736 New Member

    Hi, there is a remote api to do that, you can not insert them manually in the ispconfig database as such modifications will be ignored. There are example scripts for that in the remote_client folder of the ispconfig tar.gz.
     

Share This Page