#
#  Module LoadNetStateObjs.tcl
#
#  $Log: LoadNetStateObjs.tcl,v $
#  Revision 1.2  2006/11/15 06:52:16  vfom
#  pred port
#
#  Revision 1.1  2006/10/26 09:39:17  vfom
#  Added code for netstate server query
#
#
#
namespace  eval ::AskNetStateServer {
variable error 0
}
#
proc  AskNetStateServer { q } {
    if { $::AskNetStateServer::error } { return "" }
    if { $::netStateServer=="" || $::netStateServerPort=="" } {
    	tk_dialog .dlg "AskNetStateServer" \
	  [msgcat::mc "Undefined parameters: netStateServer or netStateServerPort.\n Check in 'Monitor->Configure' and try again."] error 0 [msgcat::mc "Approve" ]
	set ::AskNetStateServer::error 1
	return  ""
    }
    set prev_Cursor [.canvas cget -cursor ]
    .canvas configure -cursor watch
    update idletasks
    
    if { [catch { socket $::netStateServer $::netStateServerPort } ::fdAN ] } {
	.canvas configure -cursor $prev_Cursor
    	tk_dialog .dlg "AskNetStateServer" \
	  [concat [msgcat::mc "Can't open NetState Server"] : $::netStateServer $::netStateServerPort] error 0 [msgcat::mc "Approve" ]
	set ::AskNetStateServer::error 1
	return ""
    }
    puts $::fdAN "$q\n\n"
    flush $::fdAN
    set rez ""
    set str ""
    while (1) {
	gets $::fdAN str
	if { [ regexp "Invalid" $str  ] } { break }
	if { [ regexp "denied" $str  ] } {
	    tk_dialog .dlg "AskNetStateServer" \
	      $str error 0 [msgcat::mc "Approve" ]
	    break 
	}
	lappend rez $str	
    }
    close $::fdAN
    .canvas configure -cursor $prev_Cursor
    update idletasks
    return $rez
}
#
proc LoadNetStateObjs {} {
    set ::AskNetStateServer::error 0
    LoadDevTable
    # global settings

    # per object data
    set netstate_output [ AskNetStateServer "OBJECT .*" ]
    set scroll_reg [ grid bbox .grid 0 0 0 0 ]
    set curr_width [expr [lindex $scroll_reg 2] - [lindex $scroll_reg 0] ]
    set i_w [image width $::arIcons($::nodeIcon)]
    set i_h [image height $::arIcons($::nodeIcon)]
    set ::startX 0
    set ::startY 0
    # AutoPlace new nodes below all known objects
    foreach obj [.canvas find all] {
	set obj_tags [.canvas itemcget $obj -tags]
	if { [string match {bgimage} $obj_tags  ] } { continue }
	set bounds [.canvas bbox $obj ]
	set rdy [lindex $bounds 3]
	if { $rdy > $::startY } { set ::startY $rdy } 
    }
    set ::startY [expr $::startY + $::h_autoplacement ]
    set curr_obj ""
    foreach str $netstate_output {
	if [ regexp -expanded -- {^(.+)!NAME} $str nam1 nam2 nam3 ] {
	    if { ![string match $curr_obj $nam2 ] }  {
#	    	puts "Object $nam2 "
		if {![info exists ::node_key($nam2)]} {
		    set ::startX  [ expr  $::startX + $::w_autoplacement  ]
		    if { $::startX >= $curr_width } {
			set ::startX  $::w_autoplacement
			set ::startY [expr $::startY + $::h_autoplacement  ]
		    }
		
		    #                                   name     X          Y       icon         font        fontColor
		    set hostKey  [ CreateNodeNoUpdate  $nam2  $::startX $::startY $::nodeIcon  $::nodeFont  $::nodeFontColor ]
		    set ::node_descr($hostKey) ""
		} else { 
		    puts "LoadNetStateObjs: Duplicated hostname: $nam2 !(Skipped.)"
		}
		set curr_obj $nam2
	    }
	    continue
	}
	if [ regexp -expanded -- {^(.+)!ADDRESS\s*=\s*"(.+)"} $str nam1 nam2 adr2 nam3 ] {
#	    puts "Address \"$nam2\"  $adr2"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_ip($hostKey) "$adr2"
	    }
	    continue
	}
	if [ regexp -expanded -- {^(.+)!sysDescr\s*=\s*"(.+)"} $str nam1 nam2 nam3 nam4 ] {
#	    puts "sysDescr \"$nam2\"  $nam3"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_descr($hostKey) "$::node_descr($hostKey)\nSysDescr = $nam3"
	    }
	    continue
	}
	if [ regexp -expanded -- {^(.+)!sysName\s*=\s*"(.+)"} $str nam1 nam2 nam3 nam4 ] {
#	    puts "sysName \"$nam2\"  $nam3"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_descr($hostKey) "$::node_descr($hostKey)\nSysName = $nam3"
	    }
	    continue
	}
	if [ regexp -expanded -- {^(.+)!sysContact\s*=\s*"(.+)"} $str nam1 nam2 nam3 nam4 ] {
#	    puts "sysContact \"$nam2\"  $nam3"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_descr($hostKey) "$::node_descr($hostKey)\nSysContact = $nam3"
	    }
	    continue
        }
	if [ regexp -expanded -- {^(.+)!sysLocation\s*=\s*"(.+)"} $str nam1 nam2 nam3 nam4] {
#	    puts "sysLocation \"$nam2\"  $nam3"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_descr($hostKey) "$::node_descr($hostKey)\nSysLocation = $nam3"
	    }
	    continue
        }
	if [ regexp -expanded -- {^(.+)!sysObjID\s*=\s*(.+)} $str nam1 nam2 nam3 nam4 ] {
#	    puts "sysObjID \"$nam2\"  $nam3"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_descr($hostKey) "$::node_descr($hostKey)\nSysObjID = $nam3"
		if { [ info exists ::Discovery::devArray(icon,.$nam3)] } {	
		    set ::node_icon($hostKey) $::Discovery::devArray(icon,.$nam3)
		    $hostKey configure -image $::arIcons($::node_icon($hostKey))
		}
	    }
	    continue
	}	
	if [ regexp -expanded -- {^(.+)!ROUTER\s*=} $str nam1 nam2 nam3 nam4 ] {
#	    puts "ROUTER \"$nam2\""
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_methodslist($hostKey) [lappend ::node_methodslist($hostKey) [list Method router "\"$::node_getCommunity\""]]
		set ::node_methodslist($hostKey) [lappend ::node_methodslist($hostKey) [list Save router ""]]
		set ::node_snmptrapslist($hostKey) [lappend ::node_snmptrapslist($hostKey) "generic" ]
	    }
	    continue
	}
	if [ regexp -expanded -- {^(.+)!SNMP\s*=} $str nam1 nam2 nam3 nam4 ] {
#	    puts "SNMP \"$nam2\""
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_methodslist($hostKey) [lappend ::node_methodslist($hostKey) [list Method snmp "\"$::node_getCommunity\""]]
		set ::node_snmptrapslist($hostKey) [lappend ::node_snmptrapslist($hostKey) "generic" ]
	    }
	    continue
	}
	if [ regexp -expanded -- {^(.+)!POLLING\s*=(.+)} $str nam1 nam2 nam3 nam4 ] {
#	    puts "POLLING \"$nam2\" $nam3"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_poll($hostKey) $nam3
	    }
	    continue
	}
	if [ regexp -expanded -- {^(.+)!TIMEOUT\s*=(.+)} $str nam1 nam2 nam3 nam4 ] {
#	    puts "TIMEOUT \"$nam2\" $nam3"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_tmo($hostKey) $nam3
	    }
	    continue
	}
	if [ regexp -expanded -- {^(.+)!RETRIES\s*=(.+)} $str nam1 nam2 nam3 nam4 ] {
#	    puts "RETRIES \"$nam2\" $nam3"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_retr($hostKey) $nam3
	    }
	    continue
	}
	if [ regexp -expanded -- {^(.+)!SAVING\s*=(.+)} $str nam1 nam2 nam3 nam4 ] {
#	    puts "SAVING \"$nam2\" $nam3"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_dataSaveInterval($hostKey) $nam3
	    }
	    continue
	}
	if [ regexp -expanded -- {^(.+)!DATADIR\s*=\s*"(.+)"} $str nam1 nam2 nam3 nam4 ] {
#	    puts "DATADIR \"$nam2\" $nam3"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_datadir($hostKey) $nam3
	    }
	    continue
	}
	if [ regexp -expanded -- {^.+!(QUERYTIME|REPLYTIME|LASTCHANGE|HOPCOUNT|NETPATH|STATE|PING.*|)\s*=} $str nam1 nam2 adr2 nam3 ] {
#	    puts "ELSE $str"
	    continue
	}
	if [ regexp -expanded -- {^(.+)!(.+)\s*=} $str nam1 nam2 nam3 nam4 ] {
##	    puts "VAR $nam3"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ::node_snmpvarslist($hostKey) [lappend ::node_snmpvarslist($hostKey) [list $nam3 "" ""]]
	    }
	}
    }
    # interfaces
    set netstate_output [ AskNetStateServer "INTERFACE .*!NAME" ]
    foreach str $netstate_output {
	if [ regexp -expanded -- {^(.+)!(.+)!NAME\s*=\s*"(.+)"} $str nam1 nam2 nam3 nam4 nam5 ] {
#	    puts "INTERFACE \"$nam2\" $nam3 $nam4"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set ifKey       .if$::ifIndex
		set ::if_owner($ifKey) $hostKey
		set ::if_name($ifKey)  $nam3
		set ::if_descr($ifKey)      ""
		set ::if_datadir($ifKey)    ""
		set ::if_bandwidth($ifKey)  ""
		lappend ::if_methodslist($ifKey)  [list Save interface ]
	        lappend ::node_iflist($hostKey) [list $nam3 $ifKey ]							
		incr ::ifIndex
	    }
	}
    }
    set netstate_output [ AskNetStateServer "INTERFACE .*!ifAlias" ]
    foreach str $netstate_output {
	if [ regexp -expanded -- {^(.+)!(.+)!ifAlias\s*=\s*"(.+)"} $str nam1 nam2 nam3 nam4 nam5 ] {
#	    puts "INTERFACE alias \"$nam2\" $nam3 $nam4"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		foreach iflist $::node_iflist($hostKey) {
		    set ifName [lindex $iflist 0]
		    if [string match $nam3 $ifName] {
			set ifKey [lindex $iflist 1 ]
			set ::if_descr($ifKey)    $nam4
			break;
		    }
		}				
	    }
	}
    }
    set netstate_output [ AskNetStateServer "INTERFACE .*!DATADIR" ]
    foreach str $netstate_output {
	if [ regexp -expanded -- {^(.+)!(.+)!DATADIR\s*=\s*"(.+)"} $str nam1 nam2 nam3 nam4 nam5 ] {
#	    puts "INTERFACE datadir \"$nam2\" $nam3 $nam4"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		foreach iflist $::node_iflist($hostKey) {
		    set ifName [lindex $iflist 0]
		    if [string match $nam3 $ifName] {
			set ifKey [lindex $iflist 1 ]
			set ::if_datadir($ifKey)    $nam4
			break;
		    }
		}				
	    }
	}
    }    
    # bgp
    set netstate_output [ AskNetStateServer "BGPAS .*" ]
    foreach str $netstate_output {
	if [ regexp -expanded -- {^(.+)!(.+)!ASN\s*=\s*([0-9]+)} $str nam1 nam2 nam3 nam4 nam5 ] {
#	    puts "BGPAS \"$nam2\" =$nam3= =$nam4="
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set bgpKey      .bgp$::bgpIndex
		set ::bgp_owner($bgpKey) $hostKey
		set ::bgp_asn($bgpKey)  $nam4
		set ::bgp_descr($bgpKey)      ""
		set ::bgp_datadir($bgpKey)    ""
		set ::bgp_peerslist($bgpKey)  [list]		
		lappend ::bgp_methodslist($bgpKey)  [list Save bgp]
		lappend ::node_bgplist($hostKey) [list $nam4 $bgpKey ]
		incr ::bgpIndex
	    }	    
	    continue
	}
	if [ regexp -expanded -- {^(.+)!(.+)!DATADIR\s*=\s*"(.+)"} $str nam1 nam2 nam3 nam4 nam5 ] {
#	    puts "BGPDATADIR \"$nam2\" $nam3 $nam4"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		foreach slist $::node_bgplist($hostKey) {
		    set asn [join [list "AS" [lindex $slist 0]] ""]
		    if [string match $nam3 $asn] {
			set bgpKey [lindex $slist 1 ]
			set ::bgp_datadir($bgpKey)  $nam4
			break;
		    }
		}		
	    }
	}	
    }
    set netstate_output [ AskNetStateServer "BGPPEER .*!bgpPeerRemoteAddr" ]
    foreach str $netstate_output {
	if [ regexp -expanded -- {^(.+)!(.+)!(.+)!bgpPeerRemoteAddr\s*=\s*(.+)} $str nam1 nam2 nam3 nam4 nam5 nam6 ] {
#	    puts "BGPPEER ADDR \"$nam2\" $nam3 $nam5"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		foreach slist $::node_bgplist($hostKey) {
		    set asn [join [list "AS" [lindex $slist 0]] ""]
		    if [string match $nam3 $asn] {
			set bgpKey [lindex $slist 1 ]
    			if [ IsValidIpAddr $nam5 ] { lappend ::bgp_peerslist($bgpKey) $nam5 }
			break;
		    }
		}		
	    }
	}	
    }       
    # services
    set netstate_output [ AskNetStateServer "SERVICE .*" ]
    foreach str $netstate_output {
	if [ regexp -expanded -- {^(.+)!(.+)!NAME\s*=\s*"(.+)"} $str nam1 nam2 nam3 nam4 nam5 ] {
#	    puts "SERVICE \"$nam2\" $nam3 $nam4"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		set srvKey       .srv$::srvIndex
		set ::srv_owner($srvKey) $hostKey
		set ::srv_name($srvKey)  $nam3
		set ::srv_descr($srvKey)      ""
		set ::srv_datadir($srvKey)    ""
		set ::srv_methodslist($srvKey)  [list]
		set ::srv_snmpvarslist($srvKey)  [list]
		lappend ::node_srvlist($hostKey) [list $nam3 $srvKey ]
		incr ::srvIndex
	    }
	    continue    
	}				
	if [ regexp -expanded -- {^.+!.+!(STATE|QUERYTIME|REPLYTIME|LASTCHANGE)\s*=} $str nam1 nam2 nam3 nam4 ] {
	    continue
	}
	if [ regexp -expanded -- {^(.+)!(.+)!(.+)\s*=} $str nam1 nam2 nam3 nam4 nam5 ] {
#	    puts "SERVICE variable \"$nam2\" $nam3 $nam4"
	    if {[info exists ::node_key($nam2)]} {
		set hostKey $::node_key($nam2)
		foreach srvlist $::node_srvlist($hostKey) {
		    set srvName [lindex $srvlist 0]
		    if [string match $nam3 $srvName] {
			set srvKey [lindex $srvlist 1]
			set ::srv_snmpvarslist($srvKey) [lappend ::srv_snmpvarslist($srvKey) [list $nam4 "" ""]]
			break
		    }
		}    
	    }
	}
    }
    #		
    updateScrollRegion {}
}