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



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

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



113
114
115
# File 'lib/agent/util/am_util.rb', line 113

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

#decrypt(cipher_text, key) ⇒ Object



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

def decrypt cipher_text, key
    if (cipher_text == nil || key == nil)
      return nil
    end
  plain_text = nil
  begin
      key = key.length > 16 ? key[(key.length-16)..-1] : key
      cipher = OpenSSL::Cipher.new('AES-128-CBC').decrypt
      cipher.key = key
      cipher.iv = ManageEngine::APMObjectHolder.instance.constants.en_alphabets + ManageEngine::APMObjectHolder.instance.constants.en_numerals
      plain_text = cipher.update(Base64.decode64(cipher_text)) + cipher.final
  rescue Exception=>e
      plain_text = nil
  end
    return plain_text
end

#encrypt(text, key) ⇒ Object



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

def encrypt text, key
  if (text == nil || key == nil)
    return nil
  end
    cipher_text = nil
  begin
    key = key.length > 16 ? key[(key.length-16)..-1] : key
      cipher = OpenSSL::Cipher.new('AES-128-CBC').encrypt
      cipher.key = key
      cipher.iv = ManageEngine::APMObjectHolder.instance.constants.en_alphabets + ManageEngine::APMObjectHolder.instance.constants.en_numerals
      cipher_text = Base64.encode64(cipher.update(text) + cipher.final)
  rescue Exception=>e
    cipher_text = nil
  end
    return cipher_text
end

#formatStacktrace(stacktrace) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/agent/util/am_util.rb', line 167

def formatStacktrace(stacktrace)
  strace = Array.new
  
  if (stacktrace != nil)
    begin
      stacktrace.each do |stackelement|
        temp = Array.new
        temp[0] = stackelement
        temp[1] = ""
        temp[2] = ""
        temp[3] = ""
        strace.push(temp)
        if (strace.size == 10)
          break;
        end
      end
    rescue Exception=>e
      @log.logException "Error while formatting stack trace. #{e.message}", e
    end
  end
  
  strace
end

#getArray(value, sep) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/agent/util/am_util.rb', line 118

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

#getBooleanValue(str) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/agent/util/am_util.rb', line 105

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

#is_float(val) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/agent/util/am_util.rb', line 146

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

#is_integer(val) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/agent/util/am_util.rb', line 138

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

#isPortBusy(port) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/agent/util/am_util.rb', line 125

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



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/agent/util/am_util.rb', line 154

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/agent/util/am_util.rb', line 49

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
      propsFile.close
  rescue Exception=>e
    @log.info "Problem in Reading Property File :  #{e.message} "
    @log.error "#{e.backtrace}"
  end
  props
end

#setLogger(log) ⇒ Object



10
11
12
# File 'lib/agent/util/am_util.rb', line 10

def setLogger log
  @log = log
end

#writeProperties(f, props) ⇒ Object

write the Properties into the Property file



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/agent/util/am_util.rb', line 73

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