private
readonly
string
path
=
"
F:\\ftproot\\
"
;
private
readonly
string
host
=
"
192.168.138.15
"
;
private
readonly
int
port
=
2121
;
private
readonly
string
username
=
"
ftp
"
;
private
readonly
string
password
=
"
ftp
"
;
private
static
string
mcDBConnectString
=
"
Data Source=192.168.138.104\\SQL2005;Initial Catalog=UAP5MC;User ID=sa;Password=password
"
;
private
sealed
class
ScomServers
{
public
string
Name {
get
;
set
; }
public
string
Guid {
get
;
set
; }
}
private
void
button1_Click(
object
sender, EventArgs e)
{
//
生成scomlog
generateLog();
}
private
void
button2_Click(
object
sender, EventArgs e)
{
//
上传日志到FTP服务器
uploadLogs();
}
private
List
<
ScomServers
>
loadScomServers()
{
List
<
ScomServers
>
servers
=
new
List
<
ScomServers
>
();
string
sql
=
"
select s.note + '.' + s.scomserverdomain as [Name], c.guidbyscom as Guid from tblscomServerInfo s, tblcmbase_server c where s.note = c.servername AND c.guidbyscom is not null
"
;
IDataReader reader
=
GetDataReader(sql);
try
{
while
(
true
==
reader.Read())
{
servers.Add(
new
ScomServers()
{
Name
=
reader[
"
Name
"
].ToString(),
Guid
=
reader[
"
Guid
"
].ToString(),
});
}
}
finally
{
reader.Close();
}
return
servers;
}
private
void
generateLog()
{
List
<
ScomServers
>
servers
=
loadScomServers();
string
date
=
string
.Format(
"
{0:yyyyMMddHHmmssfffffff}
"
, DateTime.Now);
//
fixme: date format
DateTime update
=
DateTime.Now.AddDays(
-
1
);
DateTime from
=
new
DateTime(update.Year, update.Month, update.Day);
DateTime to
=
DateTime.Today.Date;
try
{
if
(servers
!=
null
)
{
foreach
(ScomServers server
in
servers)
{
string
sql
=
@"
select d.serverguid,d.updatetime as [DateTime],
replace(d.servername,'Microsoft.Windows.Computer:','') as ComputerName,
a.servermgip as ComputerIP,
d.cpu as CPUUsedValue,
d.memoryuse*100 as AvailablePer,
'ALL' as DiskName,
(1-d.storageuse) * 100 as FreeSpace,
(d.TotalDiskArray - d.DiskArrayUse) as FreeSpaceMB
from tblscomServerPerformanceDetail d
left join tblcmbase_Server a
on d.serverguid = a.guidbyscom
where d.serverguid = '
"
+
server.Guid
+
"
'
"
;
IDataReader reader
=
GetDataReader(sql);
try
{
StreamWriter cpuWriter
=
fileOpen(
string
.Join(
""
,
new
string
[] { path, server.Name,
"
_
"
, date,
"
_SCOM_2007_CPU.log
"
}),
string
.Join(
""
,
new
string
[] {
"
DateTime
"
,
"
\t
"
,
"
ComputerName
"
,
"
\t
"
,
"
ComputerIP
"
,
"
\t
"
,
"
CPUUsedValue
"
,
"
\t
"
,
"
PRI
"
}));
StreamWriter ramWriter
=
fileOpen(
string
.Join(
""
,
new
string
[] { path, server.Name,
"
_
"
, date,
"
_SCOM_2007_Memory.log
"
}),
string
.Join(
""
,
new
string
[] {
"
DateTime
"
,
"
\t
"
,
"
ComputerName
"
,
"
\t
"
,
"
ComputerIP
"
,
"
\t
"
,
"
AvailableMB
"
,
"
\t
"
,
"
PRI
"
}));
StreamWriter hddWriter
=
fileOpen(
string
.Join(
""
,
new
string
[] { path, server.Name,
"
_
"
, date,
"
_SCOM_2007_Disk.log
"
}),
string
.Join(
""
,
new
string
[] {
"
DateTime
"
,
"
\t
"
,
"
ComputerName
"
,
"
\t
"
,
"
ComputerIP
"
,
"
\t
"
,
"
DiskName
"
,
"
\t
"
,
"
FreeSpace
"
,
"
\t
"
,
"
FreeMB
"
,
"
\t
"
,
"
PRI
"
}));
while
(
true
==
reader.Read())
{
cpuWriter.WriteLine(
string
.Join(
""
,
new
string
[] {
reader[
"
DateTime
"
]
==
null
?
""
: reader[
"
DateTime
"
].ToString().Trim(),
"
\t
"
,
reader[
"
ComputerName
"
]
==
null
?
""
: reader[
"
ComputerName
"
].ToString().Trim(),
"
\t
"
,
reader[
"
ComputerIP
"
]
==
null
?
""
: reader[
"
ComputerIP
"
].ToString().Trim(),
"
\t
"
,
reader[
"
CPUUsedValue
"
]
==
null
?
""
: reader[
"
CPUUsedValue
"
].ToString().Trim(),
"
"
,
"
低
"
,
}));
ramWriter.WriteLine(
string
.Join(
""
,
new
string
[] {
reader[
"
DateTime
"
]
==
null
?
""
: reader[
"
DateTime
"
].ToString().Trim(),
"
\t
"
,
reader[
"
ComputerName
"
]
==
null
?
""
: reader[
"
ComputerName
"
].ToString().Trim(),
"
\t
"
,
reader[
"
ComputerIP
"
]
==
null
?
""
: reader[
"
ComputerIP
"
].ToString().Trim(),
"
\t
"
,
reader[
"
AvailablePer
"
]
==
null
?
""
: reader[
"
AvailablePer
"
].ToString().Trim(),
"
\t
"
,
//
fixme: ram data
"
低
"
,
}));
hddWriter.WriteLine(
string
.Join(
""
,
new
string
[] {
reader[
"
DateTime
"
]
==
null
?
""
: reader[
"
DateTime
"
].ToString().Trim(),
"
\t
"
,
reader[
"
ComputerName
"
]
==
null
?
""
: reader[
"
ComputerName
"
].ToString().Trim(),
"
"
,
reader[
"
ComputerIP
"
]
==
null
?
""
: reader[
"
ComputerIP
"
].ToString().Trim(),
"
\t
"
,
"
ALL
"
,
reader[
"
FreeSpace
"
]
==
null
?
""
: reader[
"
FreeSpace
"
].ToString().Trim(),
"
\t
"
,
reader[
"
FreeSpaceMB
"
]
==
null
?
""
: reader[
"
FreeSpaceMB
"
].ToString().Trim(),
"
\t
"
,
//
fixme: hdd data
"
低
"
,
}));
}
fileClose(cpuWriter);
fileClose(ramWriter);
fileClose(hddWriter);
}
finally
{
reader.Close();
}
}
}
}
catch
(Exception ex)
{
}
}
private
StreamWriter fileOpen(
string
fileName,
string
header)
{
StreamWriter writer
=
new
StreamWriter(fileName,
true
, System.Text.Encoding.UTF8);
writer.WriteLine(header);
return
writer;
}
private
void
fileClose(StreamWriter writer)
{
writer.Flush();
writer.Close();
}
private
void
uploadLogs()
{
try
{
FtpClient ftp
=
new
FtpClient(host, port);
ftp.Open(username, password);
ftp.ChangeDirectory(
"
DB
"
);
string
[] filenames
=
Directory.GetFiles(path,
"
*.log
"
);
if
(filenames
!=
null
&&
filenames.Length
>
0
)
{
foreach
(
string
filename
in
filenames)
{
if
(filename.Contains(
"
CPU
"
))
{
ftp.ChangeDirectory(
"
CPU
"
);
}
else
if
(filename.Contains(
"
Memory
"
))
{
ftp.ChangeDirectory(
"
MEM
"
);
}
else
if
(filename.Contains(
"
Disk
"
))
{
ftp.ChangeDirectory(
"
HDD
"
);
}
string
fn
=
Path.GetFileName(filename);
if
(ftp.Exists(Path.GetFileName(fn)))
{
ftp.DeleteFile(fn);
}
ftp.PutFile(filename);
File.Delete(filename);
ftp.ChangeDirectoryUp();
}
}
else
{
}
ftp.Close();
}
catch
(Exception ex)
{
}
}
#region
数据库操作
///
<summary>
///
获取数据DataSet
///
</summary>
///
<param name="sqlString">
查询SQL
</param>
///
<returns>
结果DataSet
</returns>
internal
static
DataSet GetDataSet(
string
sqlString)
{
//
return Framework.IDE.DB.ExecuteDataSet(CommandType.Text, sqlString);
DataSet ds
=
new
DataSet();
SqlConnection conn
=
new
SqlConnection(mcDBConnectString);
SqlDataAdapter adp
=
new
SqlDataAdapter(sqlString, conn);
adp.Fill(ds);
return
ds;
}
///
<summary>
///
获取数据DataSet
///
</summary>
///
<param name="sqlString">
查询SQL
</param>
///
<returns>
结果DataSet
</returns>
internal
static
object
GetDataScalar(
string
sqlString)
{
object
result
=
null
;
//
result = Framework.IDE.DB.ExecuteScalar(CommandType.Text, sqlString);
SqlConnection conn
=
new
SqlConnection(mcDBConnectString);
try
{
conn.Open();
SqlCommand cmd
=
conn.CreateCommand();
cmd.CommandText
=
sqlString;
cmd.CommandType
=
System.Data.CommandType.Text;
result
=
cmd.ExecuteScalar();
}
finally
{
conn.Close();
}
return
result;
}
internal
static
IDataReader GetDataReader(
string
sqlString)
{
IDataReader reader
=
null
;
SqlConnection conn
=
new
SqlConnection(mcDBConnectString);
try
{
conn.Open();
SqlCommand cmd
=
conn.CreateCommand();
cmd.CommandText
=
sqlString;
cmd.CommandType
=
System.Data.CommandType.Text;
reader
=
(IDataReader) cmd.ExecuteReader();
}
finally
{
}
return
reader;
}
///
<summary>
///
获取数据DataSet
///
</summary>
///
<param name="sqlString">
查询SQL
</param>
///
<returns>
结果DataSet
</returns>
internal
static
int
ExecuteNonQuery(
string
sqlString)
{
int
result
=
0
;
//
result = Framework.IDE.DB.ExecuteNonQuery(CommandType.Text, sqlString);
SqlConnection conn
=
new
SqlConnection(mcDBConnectString);
try
{
conn.Open();
SqlCommand cmd
=
conn.CreateCommand();
cmd.CommandText
=
sqlString;
cmd.CommandType
=
System.Data.CommandType.Text;
result
=
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
return
result;
}
#endregion