Beispiel #1
0
    public void checknow()
    {
        Console.WriteLine("Monitor started.\n");
        string dbaddress = null;
        Process MyProc = new Process();
        MyProc.StartInfo.FileName = "MSservice.exe";
        icmp icmp = new icmp();
        dbwork db = new dbwork();

        string newnowstate = "", newlasterror = "", newlastsuccess = "";
        DateTime newlasttime = DateTime.Parse("00:00:00");
        int tempkod = 0, errortimes = 0;
        int scanint;

        while (true)
        {
            if (db.getdbparam("config.txt").GetValue(1).ToString() != dbaddress)
            {
                dbaddress = db.getdbparam("config.txt").GetValue(1).ToString();
                Console.WriteLine("\nNew db address: " + dbaddress + "\n");
            }
            else
            {
                //check scan
                if (db.getdbparam("config.txt").GetValue(2).ToString() != "scanworknow")
                {
                    if (db.tableexist(dbaddress, "forscan"))
                    {
                        MyProc.StartInfo.Arguments = "scantd";
                        MyProc.Start();
                        Console.WriteLine("\nNew hosts found. Run scan\n");
                    }
                }

                //run monitoring
                OleDbDataReader tempread = db.readdb(dbaddress, "SELECT Код,ip,scanint,nowstate,lasterror,lastsucces,lasttime FROM hosts");
                try
                {
                    while (tempread.Read())
                    {
                        tempkod = Convert.ToInt32(tempread["Код"]);
                        try
                        {
                            scanint = Convert.ToInt32(tempread["scanint"]);
                        }
                        catch (Exception ex) { Console.WriteLine("Convert scanint error: " + ex); scanint = 0; }
                        host temp = new host(tempkod, (tempread["ip"]).ToString(), scanint, (tempread["nowstate"]).ToString(), (tempread["lasterror"]).ToString(), (tempread["lastsucces"]).ToString(), (tempread["lasttime"]).ToString());

                        if (temp.timecheck())
                        {
                            newnowstate = "";
                            newlasterror = "";
                            newlastsuccess = "";
                            newlasttime = DateTime.Parse("00:00:00");

                            Console.Write("host: " + temp.Ip + "\t");
                            if (icmp.ping(temp.Ip, 1))
                            {
                                newnowstate = "ok";
                                newlastsuccess = icmp.ping(temp.Ip);
                                newlasttime = DateTime.Now;
                                db.updatedb(dbaddress, "UPDATE hosts SET nowstate='" + newnowstate + "', lastsucces='" + newlastsuccess + "', lasttime='" + newlasttime + "' WHERE Код =" + tempkod);
                            }
                            else
                            {
                                newnowstate = "na";
                                newlasterror = DateTime.Now.ToString();
                                newlasttime = DateTime.Now;
                                db.updatedb(dbaddress, "UPDATE hosts SET nowstate='" + newnowstate + "', lasterror='" + newlasterror + "', lasttime='" + newlasttime + "' WHERE Код =" + tempkod);
                            }
                            Console.WriteLine(newnowstate);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.Write("Ошибка tempread.Read()\n" + ex + "\n" + errortimes++ + " times already");
                    if (errortimes > 3)
                    {
                        MyProc.StartInfo.Arguments = "check";
                        MyProc.Start();
                        return;
                    }
                }
                Thread.Sleep(5000);
            }
        }
    }
Beispiel #2
0
    public void checknow()
    {
        Console.WriteLine("Monitor started.\n");
        string  dbaddress = null;
        Process MyProc    = new Process();

        MyProc.StartInfo.FileName = "MSservice.exe";
        icmp   icmp = new icmp();
        dbwork db   = new dbwork();

        string   newnowstate = "", newlasterror = "", newlastsuccess = "";
        DateTime newlasttime = DateTime.Parse("00:00:00");
        int      tempkod = 0, errortimes = 0;
        int      scanint;

        while (true)
        {
            if (db.getdbparam("config.txt").GetValue(1).ToString() != dbaddress)
            {
                dbaddress = db.getdbparam("config.txt").GetValue(1).ToString();
                Console.WriteLine("\nNew db address: " + dbaddress + "\n");
            }
            else
            {
                //check scan
                if (db.getdbparam("config.txt").GetValue(2).ToString() != "scanworknow")
                {
                    if (db.tableexist(dbaddress, "forscan"))
                    {
                        MyProc.StartInfo.Arguments = "scantd";
                        MyProc.Start();
                        Console.WriteLine("\nNew hosts found. Run scan\n");
                    }
                }

                //run monitoring
                OleDbDataReader tempread = db.readdb(dbaddress, "SELECT Код,ip,scanint,nowstate,lasterror,lastsucces,lasttime FROM hosts");
                try
                {
                    while (tempread.Read())
                    {
                        tempkod = Convert.ToInt32(tempread["Код"]);
                        try
                        {
                            scanint = Convert.ToInt32(tempread["scanint"]);
                        }
                        catch (Exception ex) { Console.WriteLine("Convert scanint error: " + ex); scanint = 0; }
                        host temp = new host(tempkod, (tempread["ip"]).ToString(), scanint, (tempread["nowstate"]).ToString(), (tempread["lasterror"]).ToString(), (tempread["lastsucces"]).ToString(), (tempread["lasttime"]).ToString());

                        if (temp.timecheck())
                        {
                            newnowstate    = "";
                            newlasterror   = "";
                            newlastsuccess = "";
                            newlasttime    = DateTime.Parse("00:00:00");

                            Console.Write("host: " + temp.Ip + "\t");
                            if (icmp.ping(temp.Ip, 1))
                            {
                                newnowstate    = "ok";
                                newlastsuccess = icmp.ping(temp.Ip);
                                newlasttime    = DateTime.Now;
                                db.updatedb(dbaddress, "UPDATE hosts SET nowstate='" + newnowstate + "', lastsucces='" + newlastsuccess + "', lasttime='" + newlasttime + "' WHERE Код =" + tempkod);
                            }
                            else
                            {
                                newnowstate  = "na";
                                newlasterror = DateTime.Now.ToString();
                                newlasttime  = DateTime.Now;
                                db.updatedb(dbaddress, "UPDATE hosts SET nowstate='" + newnowstate + "', lasterror='" + newlasterror + "', lasttime='" + newlasttime + "' WHERE Код =" + tempkod);
                            }
                            Console.WriteLine(newnowstate);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.Write("Ошибка tempread.Read()\n" + ex + "\n" + errortimes++ + " times already");
                    if (errortimes > 3)
                    {
                        MyProc.StartInfo.Arguments = "check";
                        MyProc.Start();
                        return;
                    }
                }
                Thread.Sleep(5000);
            }
        }
    }
Beispiel #3
0
    public void scannow()
    {
        Console.WriteLine("Scan");

        Process MyProc = new Process();
        dbwork db = new dbwork();
        icmp temp = new icmp();
        host wkhost = new host();

        MyProc.StartInfo.FileName = "MSservice.exe";
        string dbaddress = null;
        string tempip = null;
        int errortimes = 0;

        Console.WriteLine("Read cfg");
        dbaddress = db.getdbparam("config.txt").GetValue(1).ToString();

        Console.WriteLine("Write new cfg");
        db.setdbparam("config.txt", 2, "scanworknow");

        int lastid = db.findlastkod(dbaddress, "hosts") + 1;
        int tempkod = 0;
        string tempname = "", tempgroup = "";

        Console.WriteLine("Start scan.");
        if (db.tableexist(dbaddress, "forscan"))
        {
            string[] oldhosts = db.takehosts(dbaddress);

            try
            {
                Console.WriteLine("Table exists");
                OleDbDataReader tempread = db.readdb(dbaddress, "SELECT Код,ip,grp FROM forscan");

                while (tempread.Read())
                {
                    Console.Write("Read next one: ");
                    tempkod = Convert.ToInt32(tempread["Код"]);
                    tempip = tempread["ip"].ToString();
                    Console.WriteLine(tempip);
                    tempgroup = tempread["grp"].ToString();

                    if (!wkhost.checkmatch(tempip, oldhosts))
                    {
                        Console.WriteLine("Try to ping");
                        if (temp.ping(tempip, 2))
                        {
                            Console.WriteLine("New host avalible.\nTry to resolve hostname");
                            tempname = temp.resolvename(tempip);
                            if (tempname == tempip)
                            {
                                Console.WriteLine("!New device with ip " + tempip + "\nWrite it to db.");
                            }
                            else
                            {
                                Console.WriteLine("!New device " + tempname + " with ip " + tempip + "\nWrite it to db.");
                            }

                            db.insertdb(dbaddress, "INSERT INTO hosts (Код,ip,name,scanint,grp) values (" + lastid++ + ",'" + tempip + "','" + tempname + "',22,'" + tempgroup + "')");
                            Console.WriteLine("Done.\n");
                        }
                        else
                        {
                            Console.WriteLine("No icmp device at " + tempip + "\n");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Host " + tempip + " not new\n");
                    }
                }
                Thread.Sleep(1000);

                MyProc.StartInfo.Arguments = "deltd";
                MyProc.Start();
            }
            catch (Exception ex)
            {
                Console.Write("Ошибка tempread.Read()\n" + ex + "\n" + errortimes++ + " times already");
                if (errortimes > 3)
                {
                    MyProc.StartInfo.Arguments = "scantd";
                    MyProc.Start();
                    return;
                }
            }
        }
    }
Beispiel #4
0
    public void scannow()
    {
        Console.WriteLine("Scan");

        Process MyProc = new Process();
        dbwork  db     = new dbwork();
        icmp    temp   = new icmp();
        host    wkhost = new host();

        MyProc.StartInfo.FileName = "MSservice.exe";
        string dbaddress  = null;
        string tempip     = null;
        int    errortimes = 0;

        Console.WriteLine("Read cfg");
        dbaddress = db.getdbparam("config.txt").GetValue(1).ToString();

        Console.WriteLine("Write new cfg");
        db.setdbparam("config.txt", 2, "scanworknow");

        int    lastid = db.findlastkod(dbaddress, "hosts") + 1;
        int    tempkod = 0;
        string tempname = "", tempgroup = "";

        Console.WriteLine("Start scan.");
        if (db.tableexist(dbaddress, "forscan"))
        {
            string[] oldhosts = db.takehosts(dbaddress);

            try
            {
                Console.WriteLine("Table exists");
                OleDbDataReader tempread = db.readdb(dbaddress, "SELECT Код,ip,grp FROM forscan");

                while (tempread.Read())
                {
                    Console.Write("Read next one: ");
                    tempkod = Convert.ToInt32(tempread["Код"]);
                    tempip  = tempread["ip"].ToString();
                    Console.WriteLine(tempip);
                    tempgroup = tempread["grp"].ToString();

                    if (!wkhost.checkmatch(tempip, oldhosts))
                    {
                        Console.WriteLine("Try to ping");
                        if (temp.ping(tempip, 2))
                        {
                            Console.WriteLine("New host avalible.\nTry to resolve hostname");
                            tempname = temp.resolvename(tempip);
                            if (tempname == tempip)
                            {
                                Console.WriteLine("!New device with ip " + tempip + "\nWrite it to db.");
                            }
                            else
                            {
                                Console.WriteLine("!New device " + tempname + " with ip " + tempip + "\nWrite it to db.");
                            }

                            db.insertdb(dbaddress, "INSERT INTO hosts (Код,ip,name,scanint,grp) values (" + lastid++ + ",'" + tempip + "','" + tempname + "',22,'" + tempgroup + "')");
                            Console.WriteLine("Done.\n");
                        }
                        else
                        {
                            Console.WriteLine("No icmp device at " + tempip + "\n");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Host " + tempip + " not new\n");
                    }
                }
                Thread.Sleep(1000);

                MyProc.StartInfo.Arguments = "deltd";
                MyProc.Start();
            }
            catch (Exception ex)
            {
                Console.Write("Ошибка tempread.Read()\n" + ex + "\n" + errortimes++ + " times already");
                if (errortimes > 3)
                {
                    MyProc.StartInfo.Arguments = "scantd";
                    MyProc.Start();
                    return;
                }
            }
        }
    }