Class: ManageEngine::APMUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/util/am_util.rb

Instance Method Summary collapse

Instance Method Details

#copyFiles(src, dest) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/agent/util/am_util.rb', line 47

def copyFiles src, dest
	result = false
	begin
	srcFile = File.open(src)
	destFile = File.open(dest , "w")
	destFile.write( srcFile.read(100) ) while not srcFile.eof?
	result = true
	rescue	Exception=>e
		@log.info "Problem in Copying File : \n File : #{src} to #{dest}"
		@log.logException "#{e.message}",e
		result = false;
	ensure
		srcFile.close
		destFile.close
	end

	result
end

#currenttimemillisObject



75
76
77
# File 'lib/agent/util/am_util.rb', line 75

def currenttimemillis
	(Time.now.to_f*1000).to_i
end

#getArray(value, sep) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/agent/util/am_util.rb', line 80

def getArray value,sep
	arr = Array.new
	if(value!=nil && value.length>0)
		arr = value.split(sep)
	end
	arr
end

#getBooleanValue(str) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/agent/util/am_util.rb', line 67

def getBooleanValue(str)
 	if str == true || str == "true" || str == "True" || str == "TRUE"
	return true
		else
return false
		end
end

#is_float(val) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/agent/util/am_util.rb', line 108

def is_float(val)
	Float(val)
	rescue ArgumentError
 			false
	else
 			true
end

#is_integer(val) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/agent/util/am_util.rb', line 100

def is_integer(val)
 		Integer(val)
	rescue ArgumentError
 			false
	else
 			true
end

#isPortBusy(port) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/agent/util/am_util.rb', line 87

def isPortBusy(port)
    Timeout::timeout(1) do
     begin
TCPSocket.new('localhost', port).close
        true
     rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        false
		end
		end
		rescue Timeout::Error
			false
end

#parametrizeQuery(qry) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/agent/util/am_util.rb', line 116

def parametrizeQuery qry
	begin
		qry.gsub!(/'(.*?[^'])??'/,"?")
		qry.gsub!(/\"(.*?[^\"])??\"/,"?")
		qry.gsub!(/=.\d+/,"=?")
           qry.gsub!(/,.\d+/,", ?")
	rescue Exception=>e
		@log.info "Problem in Parameterizing query:  #{e.message} "
		@log.logException "#{e.message}",e
	end
	qry
end

#readProperties(filepath) ⇒ Object

Reads the Property Files and returns a Hashes



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/agent/util/am_util.rb', line 10

def	readProperties filepath
	props = {}
	begin
		propsFile=File.open(filepath, 'r') 
    			propsFile.read.each_line do |line|
      			line.strip!
      			if (line[0] != ?# and line[0] != ?=)
        				i = line.index('=')
      	  			if (i)
  	        			props[line[0..i - 1].strip] = line[i + 1..-1].strip
         			else
          				props[line] = ''
       				end
      			end
    	  		end
	rescue Exception=>e
		@log.info "Problem in Reading Property File :  #{e.message} "
		@log.error "#{e.backtrace}"
	ensure
			propsFile.close
	end
	props
end

#setLogger(log) ⇒ Object



5
6
7
# File 'lib/agent/util/am_util.rb', line 5

def setLogger log
	@log = log
end

#writeProperties(f, props) ⇒ Object

write the Properties into the Property file



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/agent/util/am_util.rb', line 35

def writeProperties(f,props)
	begin 
		file = File.new(f,"w+")
		props.each {|key,value| file.puts "#{key}=#{value}\n"}
	rescue Exception=>e
		@log.info "Problem in Writing Property File : \n File : #{f}"
		@log.logException "#{e.message}",e
	ensure
		file.close
	end
end