Find the answer to your Linux question:
Results 1 to 6 of 6
please help me write a script that checks whether the time synchronization is correct with the NTP Server, if time difference is more then 1 minute an email is sent ...
  1. #1
    Linux Newbie
    Join Date
    Feb 2007
    Posts
    248

    script for time sync

    please help me write a script that

    checks whether the time synchronization is correct with the NTP Server, if time difference is more then 1 minute an email is sent to sysadmin with details.

    Regards

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    People around here are unlikely to want to do your scripting for you. We have lives, after all. ;)

    Read the documentation, take a stab at writing the script, show us what you have, and ask specific questions about your script. We'll be glad to help if we can, because we know that a few moments of our time will leverage into your greater knowledge of scripting and of Linux in general.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined! neirons's Avatar
    Join Date
    Nov 2006
    Location
    Latvia, Liepaja
    Posts
    18

    Smile Yea...

    And do You think is very usefull to answer on 1 Year old post. Even, if You think, author question is so wrong

  4. #4
    Linux Newbie
    Join Date
    Feb 2007
    Posts
    248
    thanks for the guidance.
    I tried, and succeed.
    now requires forums comments/suggestion if there is any mistake/weakness in the script.
    The only problem for this script is that I have to configure the "password less ssh login" for each ntp client(this script will configure to run on each ntp client)


    #!/bin/bash

    # timezone of the local network
    export zone=UZT

    export script=ntp_script@xyz.local

    # sys admin email address
    export sysadmin=abc@xyz.local

    # ldate, lmonth, lyear, lhour, lminute, lzone are client's date, month, year etc
    export ldate=$(date +%d)
    export lmonth=$(date +%m)
    export lyear=$(date +%Y)
    export lhour=$(date +%k)
    export lminute=$(date +%M)
    export lzone=$(date +%Z)

    # sdate, smonth, syear, shour, sminute, szone are the NTP Server's date, month, year etc
    # ntp server ip = 192.168.0.1
    export sdate=$(ssh 192.168.0.1 "date +%d")
    export smonth=$(ssh 192.168.0.1 "date +%m")
    export syear=$(ssh 192.168.0.1 "date +%Y")
    export shour=$(ssh 192.168.0.1 "date +%k")
    export sminute=$(ssh 192.168.0.1 "date +%M")
    export szone=$(ssh 192.168.0.1 "date +%Z")


    if [ $ldate -eq $sdate -a $lmonth -eq $smonth -a $lyear -eq $syear -a $lzone = $szone ]; then
    echo "date, month, year and timezone are OK"
    exit
    fi

    if [ $lzone != $szone -a $szone = $zone ]; then
    cp /etc/localtime /tmp
    rm -f /etc/localtime
    ln -s /usr/share/zoneinfo/Asia/Tashkent /etc/localtime
    /etc/init.d/ntp stop; sleep 3;
    ntpdate 192.168.0.1 && ntpdate 192.168.0.1 && ntpdate 192.168.0.1
    /etc/init.d/ntp start;
    # inform the admin via email
    echo "client timezone was not right, issue resolved" |mail -s "NTP issue fixed" -r $script $sysadmin <~
    exit

    else if [ $szone != $zone ]; then
    echo "NTP Server timezone is not correct" |mail -s "NTP Server Timezone wrong" -r $script $sysadmin <~
    exit
    fi

    fi

    if [ $ldate -eq $sdate -a $lmonth -eq $smonth -a $lyear -eq $syear ]; then exit
    else
    /etc/init.d/ntp stop; sleep 3;
    ntpate 192.168.0.1 && ntpdate 192.168.0.1 && ntpdate 192.168.0.1
    /etc/init.d/ntp start;
    echo "client date/time was not right, issue resolved" |mail -s "NTP issue fixed" -r $script $sysadmin <~
    fi

  5. #5
    Just Joined! neirons's Avatar
    Join Date
    Nov 2006
    Location
    Latvia, Liepaja
    Posts
    18

    Typewriting mistake in script

    Hi,

    just about line nr. 55 in Your script. Seems to be typewriting mistake
    You have:
    ntpate 192.168.0.1 && ntpdate 192.168.0.1 && ntpdate 192.168.0.1

    But right is:
    ntpdate 192.168.0.1 && ntpdate 192.168.0.1 && ntpdate 192.168.0.1

  6. #6
    Linux Newbie
    Join Date
    Feb 2007
    Posts
    248
    Quote Originally Posted by neirons View Post
    Hi,

    just about line nr. 55 in Your script. Seems to be typewriting mistake
    You have:
    ntpate 192.168.0.1 && ntpdate 192.168.0.1 && ntpdate 192.168.0.1

    But right is:
    ntpdate 192.168.0.1 && ntpdate 192.168.0.1 && ntpdate 192.168.0.1
    thanks for pointing the error

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...